Falsy in JavaScript

1. Introduction

While writing JavaScript programs, it is not mandatory for you to provide only true or false in Boolean context. For example, you can use a number or a string as a condition in if:

if (4) {
  console.log("passed"); //passed

A falsy value is a value that is considered false in a Boolean context.

JavaScript uses type coercion to coerce such values which are not true or false in the Boolean context.

2. JavaScript falsy values

Following are JavaScript falsy values:

falseThe keyword false.
0The Number zero (and values which are zero such as 0.0, etc., and 0x0).
-0The Number negative zero (and values which are -0 such as 0.0 -0.0, -0x0).
0nThe BigInt zero (and also 0x0n). Note that there is no BigInt negative zero — the negation of 0n is 0n.
“”, ”, “Empty string value.
nullthe absence of any value.
undefinedthe primitive value undefined.
NaNnot a number

3. Examples

All the following will result in bypassing of if block:

if (false)
if (0)
if (-0)
if ("")
if (null)
if (undefined)
if (0n)
if (NaN)