Skip to content

Try our new Crash Courses!

Buy one of our new Crash Courses, now hosted on Teachable.

Truthy and Falsy values – JavaScript

Length: 10 minutes

Details

Up until now, we’ve created if statements that compare two values to each other. However, it’s possible to use a single value for the condition of an if statement. Some values are considered falsy, and the computer treats them as false, while others are truthy, and are considered true.

Falsy values

Here is a list of values that are considered falsy.

  • false
  • 0
  • -0
  • 0n
  • “”
  • '' (empty single quotes)
  • “ (empty backticks)
  • null
  • undefined
  • NaN
  • document.all

Here is how falsy values work in practice.

if (0) {
  console.log("This won't print out because 0 is falsy");
}

In the example above, the line inside the if statement will not run because 0 is considered a falsy value.

Truthy values

Any value that is not listed in the list of falsy values above is considered truthy. Some examples would be any number that’s not 0 and any non-empty string.

if ("hello world") {
  console.log("This will print out because the value above is truthy");
}

In the example above, the line inside the if statement will run because “hello world” is considered truthy.

Demo

Exercises

You can use the Boolean() function to see if a value is truthy or falsy. Try the following statements in the console:

Boolean(0)

The output will be false. Then try replacing the number 0 with other values from the list of falsy values and some values that are truthy.

Reference

Falsy values on MDN

Truthy values on MDN

Back to: JavaScript Reference > JS Conditional Statements