ToBoolean
Decision making (conditional branching) always requires a boolean true or false value. But it's extremely common to want to make these decisions based on non-boolean value conditions, such as whether a string is empty or has anything in it.
_11ToBoolean(undefined); // false_11ToBoolean(null); // false_11ToBoolean(""); // false_11ToBoolean(0); // false_11ToBoolean(-0); // false_11ToBoolean(0n); // false_11ToBoolean(NaN); // false_11_11ToString(Symbol("ok")); // TypeError exception thrown_11ToNumber(42n); // TypeError exception thrown_11ToNumber(Symbol("42")); // TypeError exception thrown
Value(x) | ToString(x) | ToBoolean(x) | ToNumber(x) |
---|---|---|---|
undefined | "undefined" | false |
|
null | "null" | false |
|
"" | "" | false |
|
0 | "0" | false |
|
-0 | "0" | false |
|
0n | "0" | false |
|
NaN | "NaN" | false |
|
false | "false" | false |
|
true | "true" | true |
|
Infinity | "Infinity" | true |
|
-Infinity | "-Infinity" | true |
|
Number.MAX_VALUE | "1.7976931348623157e+308" | true |
|
Math.E | "2.718281828459045" | true |
|
"hello" | "hello" | true |
|
42 | "42" | true |
|
[1, 2, 3] | "1,2,3" | true |
|
{ a: 1 } | "[object Object]" | true |
|
42.0 | "42" | true |
|
-3 | "-3" | true |
|
"42" | "42" | true |
|
"-3" | "-3" | true |
|
"1.2300" | "1.2300" | true |
|
" 8.0 " | " 8.0 " | true |
|
"123px" | "123px" | true |
|
"hello" | "hello" | true |
|
" " | " " | true |
|
[[]] | "" | true |
|
[[[], [], []], []] | ",,," | true |
|
[, , , ,] | ",,," | true |
|
[""] | "" | true |
|
["0"] | "0" | true |
|
["-0"] | "-0" | true |
|
[null] | "" | true |
|
[null, null, null] | ",," | true |
|
[undefined] | "" | true |
|
[undefined, undefined, undefined] | ",," | true |
|
[[[[]]]] | "" | true |
|
{valueOf(){return 3}} | "[object Object]" | true |
|
{toString(){return 10}} | "10" | true |
|
{ valueOf(){return 3}, toString(){return 10} } | "10" | true |
|
@ragavkumarv
swipe to next ➡️