Blog

method

JS

Abstract Operations

Which is better to use isNan() or Number.isNaN()?

Ragav Kumar V

Ragav Kumar V

Sep 28, 2022 — Updated Sep 29, 2022 · 1 min read

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.


_11
ToBoolean(undefined); // false
_11
ToBoolean(null); // false
_11
ToBoolean(""); // false
_11
ToBoolean(0); // false
_11
ToBoolean(-0); // false
_11
ToBoolean(0n); // false
_11
ToBoolean(NaN); // false
_11
_11
ToString(Symbol("ok")); // TypeError exception thrown
_11
ToNumber(42n); // TypeError exception thrown
_11
ToNumber(Symbol("42")); // TypeError exception thrown

Value(x)ToString(x)ToBoolean(x)ToNumber(x)
undefined"undefined"
false
NaN
null"null"
false
0
""""
false
0
0"0"
false
0
-0"0"
false
0
0n"0"
false
0
NaN"NaN"
false
NaN
false"false"
false
0
true"true"
true
1
Infinity"Infinity"
true
Infinity
-Infinity"-Infinity"
true
-Infinity
Number.MAX_VALUE"1.7976931348623157e+308"
true
1.7976931348623157e+308
Math.E"2.718281828459045"
true
2.718281828459045
"hello""hello"
true
NaN
42"42"
true
42
[1, 2, 3]"1,2,3"
true
NaN
{ a: 1 }"[object Object]"
true
NaN
42.0"42"
true
42
-3"-3"
true
-3
"42""42"
true
42
"-3""-3"
true
-3
"1.2300""1.2300"
true
1.23
" 8.0 "" 8.0 "
true
8
"123px""123px"
true
NaN
"hello""hello"
true
NaN
" "" "
true
0
[[]]""
true
0
[[[], [], []], []]",,,"
true
NaN
[, , , ,]",,,"
true
NaN
[""]""
true
0
["0"]"0"
true
0
["-0"]"-0"
true
0
[null]""
true
0
[null, null, null]",,"
true
NaN
[undefined]""
true
0
[undefined, undefined, undefined]",,"
true
NaN
[[[[]]]]""
true
0
{valueOf(){return 3}}"[object Object]"
true
3
{toString(){return 10}}"10"
true
10
{ valueOf(){return 3}, toString(){return 10} }"10"
true
3
@ragavkumarv
swipe to next ➡️