Truthy and Falsy: When All is Not Equal in JavaScript

By | | JavaScript

6

JavaScript-truthy-falsyLike most computer languages, JavaScript supports Boolean data types; values which can be set to true or false. In addition, everything in JavaScript has an inherent Boolean value, generally known as either truthy or falsy. Handling truthy and falsy values can be a little quirky, especially when comparing variables. Understanding some of the more bizarre rules can help when debugging complex client-side applications.

Truthy and Falsy Values

The following values are always 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, empty arrays, and empty objects.


var a = !!(0); // variable is set to false
var b = !!("0"); // true

Comparing Falsy Values

Falsy values follow some slightly odd comparison rules which can lead to errors in program logic.

The falsy values false, 0 (zero), and "" (empty string) are all equivalent and can be compared against each other:


var c = (false == 0); // true
var d = (false == ""); // true
var e = (0 == ""); // true

The falsy values null and undefined are not equivalent to anything except themselves:


var f = (null == false); // false
var g = (null == null); // true
var h = (undefined == undefined); // true
var i = (undefined == null); // true

Finally, the falsy value NaN is not equivalent to anything — including NaN!


var j = (NaN == null); // false
var k = (NaN == NaN); // false

You should also be aware that typeof(NaN) returns "number". Fortunately, the core JavaScript function isNaN() can be used to evaluate whether a value is NaN or not.

If in doubt…
Use strict equal (===) and strict not equal (!==) in situations where truthy or falsy values could lead to logic errors. These operators ensure that the objects are compared by type and by value.


var l = (false == 0); // true
var m = (false === 0); // false

Has truthy or falsy logic ever caused you grief in JavaScript code?

The Ultimate JavaScript Bundle: 2 books + 1 course

Buy now $39 Normally $117 - save 66%

Or get access to all SitePoint's Premium Content with a Learnable membership

Craig Buckler

Craig is a Director of OptimalWorks, a UK consultancy dedicated to building award-winning websites implementing standards, accessibility, SEO, and best-practice techniques.

More Posts - Website

{ 6 comments }

Craig Buckler July 2, 2009 at 10:56 pm

@Robert Katić
Yep – you’re right. Well spotted – I’ve fixed the article. It makes you wonder why undefined and null were both implemented. The difference is fairly subtle and uninitialized variables could have been set to null.

@Jaffa The Cake
Certainly !!([]) returns true. I’m not surprised that ([]==true) fails, but then you’d expect ([]==false) to fail too?!

It just goes to show how careful you need to be. It would not surprise me if there were inconsistencies between browsers too.

Robert Katić July 2, 2009 at 10:14 pm

(undefined == null) gives true.

Jaffa The Cake July 2, 2009 at 8:23 pm

The true & false thing gets a bit weird with objects and arrays…

if ( [] ) {
  // this runs
}

if ( [] == true ) {
  // this does not run
}

if ( [] == false ) {
  // this does run
}

Jake.

Anonymous July 1, 2009 at 11:09 am

I thought === was a php specific operator. Learn something new everyday :)

lotrgamemast July 1, 2009 at 7:39 am

I’ve gotten into the habbit of always using the strict compares when writing javascript.

It also catches stuff like:

if ( “0″ == 0 ) // true
if ( “0″ === 0 ) // false

So you know you’re getting the right data type.

Balaji July 1, 2009 at 7:13 am

“Great article! Thanks for taking the time to explain Truth and Falsy values.

Comments on this entry are closed.