Skip to main content

Falsy JavaScript

In continuation of my previous blog on the basic JavaScript expressions, this blog is about Falsy and Truthy values in JavaScript.

When presented with a non boolean expression instead of a boolean value, JavaScript uses Truthy or Falsy values instead to determine the result. At first glance, this could be confusing and could lead to errors while scripting.

var a = "someval";
var b;
if (a) { // this is a truthy
    // Block is executed
if (b) { // b is undefined and so a falsy
    // Block is not executed

The Falsy Values
All expressions, objects and values are Truthy by default. There are exceptions and they are called the Falsy values and are listed below:
  # The empty string; ""
  # null
  # undefined
  # The number 0
  # NaN
  # The boolean false

It gets tricky
If you see the below table, you will note that it gets tricky or rather one should be a little extra careful while dealing with Falsy values.

Falsy Values
Truthy Values
Complicating things now
Double Negation
Logical comparison
null == nulltrue
null == undefinedtrue
null == NaNfalse
null == ""false
null == 0false
null == falsefalse
undefined == undefinedfalse
undefined == NaNfalse
undefined == ""false
undefined == 0false
undefined == falsefalse
NaN == NaNfalse
NaN == ""false
NaN == 0false
NaN == falsefalse
"" == ""true
"" == 0true
"" == falsetrue
0 == 0true
0 == falsetrue
false == falsetrue
Strict Logical comparison
null === nulltrue
"" === ""true
"" === 0false
"" === falsefalse
0 === 0true
0 === falsefalse
false === falsetrue
[ ]true
![ ]false
[ ] == truefalse

To remember:
# An empty array evaluates to Truthy whereas any boolean operation on an empty array results in a Falsy
# NaN is not equal to anything, not even itself
# Undefined is not equal to anything, not even itself
# Use strict equals (===) to be extra sure while comparing. This compares by type and value
# Avoid using Falsy value and stick to plain  true or  false to be sure of the results.

Now that you have read this far, Congratulations! I expect 0 bugs from your code henceforth :)


  1. thanks for the comment Joe .. and thanks for the link too


Post a Comment

Popular posts from this blog

Using duplicate IDs in HTML

Well today I'm being a bit controversial. Let us see what the HTML5 spec says about unique IDs in a HTML file. The  id  attribute specifies its element's  unique identifier (ID) . The value must be unique amongst all the IDs in the element's  home subtree  and must contain at least one character. The value must not contain any  space characters . An element's  unique identifier  can be used for a variety of purposes, most notably as a way to link to specific parts of a document using fragment identifiers, as a way to target an element when scripting, and as a way to style a specific element from CSS. Yes its been mentioned almost everywhere on the planet that ID must be unique. Now let us look at the below code, Launch dup.css #p2 {   background-color: yellow;  } dup-id.html <!DOCTYPE html> <html>   <head>     <title>Duplicate ID Tester</title> <link rel="stylesheet" href="dup.css" />  

Minimal required code in HTML5

I've encountered this question repeatedly of late. "What are the tags required at bare minimum for a html file?" Earlier there were a bunch of mandatory tags that were required for any html file. At bare minimum, the recommended structure was: (ref: ) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ""> <HTML>   <HEAD>     <TITLE>A small HTML</TITLE>   </HEAD>   <BODY>     <P>Small HTML file!</P>   </BODY> </HTML> Yes, using capitals for the tags was the way to go! Those were the days of the purists and strict was the way to be. Now open your notepad and copy the above code, save the file as old.html and launch it in Chrome or Firefox. You will see only one line "Small HTML file!" shown. Now launch the developer tools in Chrome or Inspect Element in Firefox. Thi

My Bookmarks fly to the Cloud

Updated: 15-Mar-2012 With latest version of Google Chrome (I'm using 17.x), you can now directly sign into Chrome from Options->Peronsal and access your bookmarks across computers. You no longer would need to export the bookmarks to Firefox using the Google Toolbar for Firefox, which is no longer supported by Google. The Google Toolbar is now available only for IE. Original (dated) article below ... ------- I have a queer problem of too many! I surf a lot, I subscribe to a lot and I bookmark a lot. Some numbers: 1 laptop and 1 netbook at home, 1 laptop and 1 desktop at office, sometimes its my Kindle and definitely my N900 when I'm on the move. So 6 devices out there and usually 2 browsers on most of them (yes Kindle only as one webkit based experimental implementation). So my bookmarks are all over the place, scattered over at least 10 different places. And each time I'm working on one box, I wish I had access to the bookmark I made a few hours ago on the other