By Craig Buckler

Truthy and Falsy: When All is Not Equal in JavaScript

By Craig Buckler

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?

  • Balaji

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

  • 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.

  • Anonymous

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

  • Jaffa The Cake

    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


  • Robert Katić

    (undefined == null) gives true.

  • @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.

Get the latest in JavaScript, once a week, for free.