Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to check if a variable is true or false in javascript

/* 
  A variables value can be an actual boolean like "true" / "false"
  or it can be of another type like string or array which can be evaluated as "truthy" / "falsy".
  Truthy: https://developer.mozilla.org/de/docs/Glossary/Truthy
  Falsy: https://developer.mozilla.org/de/docs/Glossary/Falsy
*/

// Independent check
if (myVar) {
	// entered if myVars value is either a boolean of value "true" or a value evaluated as "truthy"
}

// true/false check
if (typeof myVar === "boolean" && myVar) {
	// entered if myVars value is an actual boolean and if this boolean is "true"
}

// truthy/falsy check
if (typeof myVar !== "boolean" && myVar) {
	// entered if myVars value is not an actual boolean but if it's evaluated as "truthy"
}
 
PREVIOUS NEXT
Tagged: #check #variable #true #false #javascript
ADD COMMENT
Topic
Name
2+8 =