13
Value to Boolean Conversion
2021-02-10 - JavaScript
1// JS Tip of the Day
2// Value to Boolean Conversion
3
4// with Not Not Operator
5console.log(!!false); // false
6console.log(!!true); // true
7console.log(!!0); // false
8console.log(!!parseInt("Batman")); // false because NaN is falsy
9console.log(!!1); // true
10console.log(!!-1); // true -1 is truthy
11console.log(!!(1/0)); // true because Infinity is truthy
12console.log(!!(Infinity)); // true because Infinity is truthy
13console.log(!!""); // false because empty string is falsy
14console.log(!!"Batman"); // true because non-empty string is truthy
15console.log(!!undefined); // false because undefined is falsy
16console.log(!!null); // false because null is falsy
17console.log(!!{}); // true because an (empty) object is truthy
18console.log(!![]); // true because an (empty) array is truthy;
19
20// or use a built-in Boolean function
21console.log(Boolean("Batman")); // true because non-empty string is truthy
22// ...
23
24
25
26
27
#JStipoftheday