javascript

Truthy and Falsy in JavaScript

/*In JavaScript, truthy are expressions which evaluates to boolean true value and falsy evaluates to boolean false value. Unlike other languages, true and false values are not limited to boolean data types and comparisons. It can have many other forms. */


//Falsy Values
false
0 Both positive and negative
0n BigInt, when used as a boolean, follows the same rule as a Number
''
null
undefined
NaN
let a = false
    let b = 0
    let c = -0
    let d = 0n
    let e = ''
    let f = null
    let g = undefined
    let h = NaN
    console.log(Boolean (a))
    console.log(Boolean (b))
    console.log(Boolean (c))
    console.log(Boolean (d))
    console.log(Boolean (e))
    console.log(Boolean (f))
    console.log(Boolean (g))
    console.log(Boolean (h))

//Truthy Values
true
{} An object (whether empty or not)
[] An array (whether empty or not)
25 Numbers (whether positive or negative)
'true' Non empty strings
'false' Non empty strings
new Date() Date object
12n BigInt, when used as a boolean, follows the same rule as a Number
Infinity
Was this helpful?