Click here to Skip to main content
13,899,693 members
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 21 Jan 2014
Licenced CPOL

Truthy Vs Falsy Values in JavaScript

, 21 Jan 2014
Rate this:
Please Sign up or sign in to vote.
Truthy Vs Falsy Values in JavaScript


This article describes the truthy and falsy values of JavaScript, what the difference is between them and some exceptions and so on. In simple and short words, it’s a boolean type but it’s not like true or false only. It’s more than that. Understanding them in theory is easy but sometimes leads to big troubles when these values are compared. So let’s demystify these values.

Truthy Value

JavaScript defines the truthy value. In very few words, any value that is not falsy is a truthy value. Even if you are writing “False” then that is also a truthy value. “0? (zero in quotes) is also a truthy value.

Falsy Value

In JavaScript, the following values are considered to be falsy values:

  • false
  • 0
  • null
  • “” (empty string)
  • NaN (Not a Number)
  • #ff0000

All other values of JavaScript can be safely assumed to be truthy values. Even an empty function, empty array and empty objects are also truthy.

Comparing Falsy and Truthy values

The need for learning these values is because of its use in comparison. The comparison is where these values creates many troubles. Misinterpretation of any value may lead to the wrong action in a script. Therefore it is necessary to check for truthy and falsy values correctly.

The Falsy values 0, “” (empty string) and false are equal to each other and comparison among them results in a true value.

Null and undefined are both falsy values but are not equal to any value, not even falsy. A bit strange but that’s true.

Null and undefined are equal to themselves.

And the last one that is superior or a VVIN number in JavaScript is the number that is Not a Number! Yes! it’s none other then NaN. This Mr. is not equal to any one, not even to his duplicate (Other NaN). Any comparison made with NaN will lead the result to false and the entire script is ruined.

Now the question is, how to check for this very, very important number. To save us from this NaN devil, JavaScript provides the isNaN() function. This function can handle the expressions returning NaN.

Using “===”

To avoid the logical error in a script, you can use triple equal (===) or triple not equal(!==). Unlike a double equal (==) that uses a value for the equality check, triple equal (===) checks on the basis of value and type.


From the preceding article, I can suggest that you always use a triple equal since it is difficult to memorize the rules of double equal and truthy falsy comparison results. Thanks for reading this article. Don’t forget to share and comment.

Filed under: CodeProject, JavaScript


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Arpit Jain
India India
No Biography provided

You may also be interested in...

Comments and Discussions

Questionconsole.log(0 == '0' should evaluate to false) Pin
The Programming Guy26-Jul-16 19:41
memberThe Programming Guy26-Jul-16 19:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 21 Jan 2014
Article Copyright 2014 by Arpit Jain
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid