falsy values javascript code example
Example 1: javascript falsy values
the number 0
the BigInt 0n
the keyword null
the keyword undefined
the boolean false
the number NaN
the empty string "" (equivalent to '' or ``)
Example 2: javascript falsy
// Falsy values:
0
''
undefined
null
NaN
Example 3: falsy values javascript
false //The keyword false.
0 //The Number zero (so, also 0.0, etc., and 0x0).
-0 //The Number negative zero (so, also -0.0, etc., and -0x0).
0n, -0n //The BigInt zero and negative zero (so, also 0x0n/-0x0n).
"", '', `` //Empty string value.
null //the absence of any value.
undefined //the primitive value.
NaN //not a number.
document.all
//Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot.
//That slot only exists in document.all and cannot be set using JavaScript.
Example 4: js falsy values
let a = false
let b = 0
let c = -0
let d = 0n
let e = ''
let f = null
let g = undefined
let h = NaN
Example 5: Truthy and Falsy js
//Checking truthy and falsy value
function truthyOrFalsy (val) {
if(val) {
return true
} else {
return false
}
}
console.log(truthyOrFalsy(0)) // print false
console.log(truthyOrFalsy(5)) // print true
Example 6: is null falsy javascript
/*
Yes.
Full list of falsy values:
Anything evaluated to be 0
'', "", or ``
null
undefined
NaN
Obviously false
Big integers relative to 0n
-------------------------------------------------------------------------
To clarify, line 31 will print false.
*/
var someCheckIsTrue = false;
const checks = [
0,
'',
"",
``,
null,
undefined,
NaN,
false,
0n
];
for (const check of checks) {
if (check) {
someCheckIsTrue = true;
}
}
console.log(someCheckIsTrue);