1// JS Essentials: Falsy Values
2
3false
4undefined
5null
6NaN
70 or +0 or -0
8"" or '' or `` (empty string)
9
10// Everything else is truthy
1false //The keyword false.
20 //The Number zero (so, also 0.0, etc., and 0x0).
3-0 //The Number negative zero (so, also -0.0, etc., and -0x0).
40n, -0n //The BigInt zero and negative zero (so, also 0x0n/-0x0n).
5"", '', `` //Empty string value.
6null //the absence of any value.
7undefined //the primitive value.
8NaN //not a number.
9
10document.all
11//Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot.
12//That slot only exists in document.all and cannot be set using JavaScript.
1 let a = false
2 let b = 0
3 let c = -0
4 let d = 0n
5 let e = ''
6 let f = null
7 let g = undefined
8 let h = NaN
1if (true)
2if ({})
3if ([])
4if (42)
5if ("0")
6if ("false")
7if (new Date())
8if (-42)
9if (12n)
10if (3.14)
11if (-3.14)
12if (Infinity)
13if (-Infinity)
14
1/*
2 Yes.
3 Full list of falsy values:
4 Anything evaluated to be 0
5 '', "", or ``
6 null
7 undefined
8 NaN
9 Obviously false
10 Big integers relative to 0n
11 -------------------------------------------------------------------------
12 To clarify, line 31 will print false.
13*/
14var someCheckIsTrue = false;
15const checks = [
16 0,
17 '',
18 "",
19 ``,
20 null,
21 undefined,
22 NaN,
23 false,
24 0n
25];
26for (const check of checks) {
27 if (check) {
28 someCheckIsTrue = true;
29 }
30}
31console.log(someCheckIsTrue);