JavaScript: Truthy vs Falsy

JavaScript: Truthy vs Falsy

ยท

1 min read

A Falsy value is a value that is considered false when encountered in a boolean context.

The following table provides a complete list of Javascript falsy values:

ValueDescription
falsekeyword false
0number 0, 0.0, 0*0, 0*n
-0number -0, -0.0, -0*0
0nBigInt
"", '', ``Empty string
nullabsence of any value
undefinedprimitive value
NaNnot a number

Examples:

if (false){
    //Not reachable
}

if (0 || -0){
    //Not reachable
}

if (0n){
    //Not reachable
}

if (""){
    //Not reachable
}

if (null || undefined || NaN){
    //Not reachable
}

Truthy

A Truthy is a value that is considered true when encountered in boolean context. All values are true unless they are defined as false i.e., all values are truthy except falsey such as null, undefined, NaN, false, 0, -0, 0n, " ".

if (true){
    // reachable
}

if (1 || 2){ // any number other that 0 and -0
    // reachable
}

if ("0"){
    // reachable
}

if ("false"){
    // reachable
}

if (Infinity || -Infinity){
    // reachable
}
ย