The JavaScript Programming Language

Booleans

Boolean values can be true or false .

 

false value is returned by: undefined, null, NaN, 0, -0, ""

//falsy values 040bool_falsy.js

console.log(Boolean(undefined));
console.log(Boolean(null));
console.log(Boolean(NaN));
console.log(Boolean(0));
console.log(Boolean(-0));
console.log(Boolean("")); //empty string returns false too
console.log(Boolean('')); //empty string (single quotes)

 

//some truthy values   040bool_truthy.js

//The following returns true
console.log(Boolean(3));
console.log(Boolean('Some string'));
console.log(Boolean("Another string"));
console.log(Boolean({})); //empty object
console.log(Boolean([])); //empty array

 

 

Comparison operators: == , === , != , !=== , < , <= , > , >=

Example with ordinary and strict comparison: 041bool_comparison.js

 

Conditional operator ? (short version of IF)

Example: 042bool_conditional_operator.js

 

 

Logic operators: && , || , !

Example: 043bool_logic_operators.js

 

 The two cases where function will not be executed:

false && foo()    //always false
true || foo()   //always true

 

Example: 044bool_not_exe_function.js