JavaScript True, False: Checking for Boolean Values

Checking if a value is true or false is simple in JavaScript. All values evaluate to true, except for:

0
-0
null
undefined
NaN
empty string
false

For example, the following results in false being output since the empty string evaluates to false.

var testVar = ''
if (testVar)
document.write('true');
else
document.write('false');

It should also be noted that the literal strings '0' and 'false' evaluate to true.

Leave a Reply