Understanding NaN, isNaN() and Number.isNaN()

Note: Be careful while using “+” as this can be used for both string concatenation and number addition.

isNaN("12")
Step1: isNaN(Number("12")
Step2: isNaN(12)
Step3: 12 === NaN?
False
isNaN("apple")
Step1: isNaN(Number("apple")
Step2: isNaN(NaN) // Number("apple") gives NaN
Step3: NaN === NaN
True
Number.isNaN("12")
Step1: 12 === NaN
False
Number.isNaN("apple")
Step1: "apple" === NaN
False
Number.isNaN(NaN)
Step1: NaN === NaN
True
Number.isNaN(Number("12"))
Step1: isNaN(12)
Step2: 12 === NaN
False
Number.isNaN(Number("pikachu"))
Step1: isNaN(NaN) // Number("pikachu") returns NaN
Step2: NaN === NaN
True

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store