JavaScript Falsy & Truthy

by Ryan Morris

JavaScript

// the following all evaluate to falsy

false;
0; //zero
""; //empty string
null;
undefined;
NaN; // a special Number value meaning Not-a-Number!

// All other values are truthy, including 
"0"; // zero in quotes
"false"; // false in quotes

// empty functions
var emptyFunction = function () {};
// empty arrays
var emptyArray = [];
// empty objects
var emptyObject = {};

// all objects are truthy, therefore this will evaluate true:
if (new Boolean(false)) {
    console.log("Boolean false evaluated true");
}

//false, 0 and "" are all equivalent and can be compared against eachother

false == 0; // true
false == ""; // true
0 == ""; // true

//null and undefined are NOT equivalent to anything except themselves

null == false; // false
null == null; // true
undefined == undefined; // true
undefined == null; // true

//NaN is not equivalent to anything, including NaN

NaN == null; // false
NaN == NaN; // false