What are the falsy values in TypeScript and JavaScript?

November 12, 2021 - 3 min read

Falsy values are those values that get coerced to the boolean type false in TypeScript and JavaScript.

These are the following falsy values in TypeScript and JavaScript:

  • 0
  • false
  • NaN
  • "" (empty string)
  • 0n (bigint version of zero)
  • null
  • undefined

All the above values when used with an if conditional gets coerced to the boolean type false.

To understand it correctly, let's use an if conditional statement check and use all the above falsy values.

0 value example

// using the "0" value in an if conditional statement
if (0) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
  • In the above code since the 0 value is a falsy value, the else block will be executed and the output of else block is executed is shown to the console

false value example

// using the "false" value in an if conditional statement
if (false) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
  • In the above code since the false value is a falsy value, the else block will be executed and the output of else block is executed is shown to the console

NaN value example

// using the "NaN" value in an if conditional statement
if (NaN) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
  • In the above code since the NaN value is a falsy value, the else block will be executed and the output of else block is executed is shown to the console

"" (empty string) example

// using the empty string value in an if conditional statement
if ("") {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
  • In the above code since the "" value is a falsy value, the else block will be executed and the output of else block is executed is shown to the console

0n value example

// using the `0n` value in an if conditional statement
if (0n) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
  • In the above code since the 0n value is a falsy value, the else block will be executed and the output of else block is executed is shown to the console

null value example

// using the `null` value in an if conditional statement
if (null) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
  • In the above code since the null value is a falsy value, the else block will be executed and the output of else block is executed is shown to the console

undefined value example

// using the `undefined` value in an if conditional statement
if (undefined) {
  console.log("if block is executed");
} else {
  console.log("else block is executed");
}

// OUTPUT: else block is executed
  • In the above code since the undefined is a falsy value, the else block will be executed and the output of else block is executed is shown to the console

See the above codes live in codesandbox.

That's all 😃!

Feel free to share if you found this useful 😃.