"Truthy" and "Falsy" value in JavaScript.
When I am taking about truthy and falsy value, I am talking about Boolean type in JavaScript. As computer only understand 0 or 1, true or false so, JVM also understand only truthy or falsy value.
What are these?
- JVM only need to understand about truthy or falsy values, at the time of evaluation in Boolean context of expression or statement.
- These truthy or falsy values may not be strictly Boolean true or false but it treated as Boolean value in conditional statement.
All 6 Falsy values
- Empty string (' ')
- Boolean false
- Numeric zero (0)
- NaN (Not a Number)
- null
- undefined
Except these 6 values all other values are treated as Truthy value in JavaScript.
console. log(0, Boolean(0)) // false
console. log(undefined, Boolean(undefined)) // false
console. log(null, Boolean(null)) // false
console. log(NaN, Boolean(NaN)) // false
console. log('', Boolean('')) // false
console. log(false, Boolean(false)) // false
console.log(1, Boolean(1)) // true
console. log(-1, Boolean(-1)) // true
console. log('dct',Boolean('dct')) // true
console. log([], Boolean([])) // true
console. log({}, Boolean({})) // true
console. log(true, Boolean(true)) // true
Correct me if I am wrong!!!
Top comments (0)