Javascript - Boolean (Truthy|Falsy)

About

Data Type - Boolean in Javascript.

Declaration

var myVariable = true;

Truthy / Falsy

JavaScript defines a list of specific values that are considered “falsy” because when coerced to a boolean, they become false. Any other value not on the “falsy” list is automatically “truthy” — when coerced to a boolean they become true. Truthy values include things like 99.99 and “free”.

Truthy Falsy
Evaluate to true Evaluate to False
true false
Non-zero number 0 (zero)
“Non Empty String” “” (empty string)
Objects null
Array undefined
Functon NaN

Example: Operators such as if, ||, and && work with any values thanks to this coercion:

x = [true, false, 0, 3, "Non-Empty","", null, {}, undefined, [], NaN, function(){} ];

for (i in x) {
   if (x[i]) {
     console.log(x[i]+" is True");
   } else {
     console.log(x[i]+" is False");
   }
}

Conversion

From String

let booleanValue = (value === 'true');

because

console.log(Boolean("false"));

Typeof

console.log(typeof true);

Documentation / Reference





Discover More
Booelan Logic Hair
Boolean - (Logical) Operator (OR, AND, XOR, )

Boolean operator manipultes truthy and falsy values that can come from: a Boolean value the interpretation of a non-boolean value (ie javascript (truthy/falsy)) or the result of a comparison expression....
Javascript - Coercion

in Javascript Policy: Make the conversions explicit and use the strict equality operator. Operators such as if, ||, and && accept any values thanks to the Truthiness/Falsiness coercion. See...
Javascript - Logical Operator

Logical operator in Javascript &&: AND Returns expr1 if it can be converted to true; otherwise, returns expr2. See Usage Example: Initialization even if it's a null object Snippet example:...
Javascript - Primitive Data Type

Javascript has 6 primitive data type. string number boolean null undefined symbol Except for null and undefined, all primitive values have object equivalents that wrap around the primitive...
Javascript - Type

Type in Javascript Javascript is not static typed but it exist Javascript wrapper language that implements it. See Only values have types in JavaScript; variables are just simple containers for those...



Share this page:
Follow us:
Task Runner