Javascript - Boolean (Truthy|Falsy)

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > ECMAScript - Grammar Standard

1 - About

boolean in Javascript.

Advertising

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

Advertising

5 - Documentation / Reference

web/javascript/es/boolean.txt · Last modified: 2019/02/16 17:40 by gerardnico