About
Data Type - Boolean in Javascript.
Articles Related
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);