Javascript - Boolean (Truthy|Falsy)

1 - About

Data Type - Boolean in Javascript.

3 - Declaration

var myVariable = true;

4 - 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");

5 - Documentation / Reference

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap