Sitemap

isNaN() in JavaScript.

3 min readApr 14, 2023

--

Press enter or click to view image in full size

The ๐ข๐ฌ๐๐š๐() function is a ๐›๐ฎ๐ข๐ฅ๐ญ-๐ข๐ง ๐Ÿ๐ฎ๐ง๐œ๐ญ๐ข๐จ๐ง in JavaScript that is used to determine whether a ๐•๐€๐‹๐”๐„ is ๐๐š๐ (๐๐จ๐ญ-๐š-๐๐ฎ๐ฆ๐›๐ž๐ซ) or not.

๐“๐ก๐ž ๐ข๐ฌ๐๐š๐() ๐ฆ๐ž๐ญ๐ก๐จ๐:
1-returns a ๐›๐จ๐จ๐ฅ๐ž๐š๐ง ๐ฏ๐š๐ฅ๐ฎ๐ž (true or false).
2-return ๐ญ๐ซ๐ฎ๐ž if a value is NaN, otherwise ๐Ÿ๐š๐ฅ๐ฌ๐ž.
3-๐œ๐จ๐ง๐ฏ๐ž๐ซ๐ญ๐ฌ ๐ญ๐ก๐ž ๐•๐€๐‹๐”๐„ ๐“๐Ž ๐€ ๐๐”๐Œ๐๐„๐‘ ๐›๐ž๐Ÿ๐จ๐ซ๐ž ๐ญ๐ž๐ฌ๐ญ๐ข๐ง๐  ๐ข๐ญ.

๐’๐ฒ๐ง๐ญ๐š๐ฑ:
isNaN(value)

๐‡๐จ๐ฐ ๐ข๐ฌ๐๐š๐() ๐ฐ๐จ๐ซ๐ค๐ฌ?

Get Muhammad Faraz Aliโ€™s stories inย yourย inbox

Join Medium for free to get updates fromย thisย writer.

When the ๐ข๐ฌ๐๐š๐() function is called with an ๐š๐ซ๐ ๐ฎ๐ฆ๐ž๐ง๐ญ ๐ญ๐ก๐š๐ญ ๐ข๐ฌ ๐ง๐จ๐ญ ๐š ๐ง๐ฎ๐ฆ๐›๐ž๐ซ, ๐ข๐ญ ๐Ÿ๐ข๐ซ๐ฌ๐ญ ๐ญ๐ซ๐ข๐ž๐ฌ ๐ญ๐จ ๐œ๐จ๐ง๐ฏ๐ž๐ซ๐ญ ๐ญ๐ก๐ž ๐š๐ซ๐ ๐ฎ๐ฆ๐ž๐ง๐ญ ๐ญ๐จ ๐š ๐ง๐ฎ๐ฆ๐›๐ž๐ซ. If the argument can be converted to a valid number, the function returns ๐Ÿ๐š๐ฅ๐ฌ๐ž, indicating that the argument is not ๐๐š๐.

E.g isNaN(๐ฏ๐š๐ฅ๐ฎ๐ž) ->isNaN(๐๐ฎ๐ฆ๐›๐ž๐ซ(value))

Examples:

//JavaScript Surgeon

//A-Numbers
isNaN(2); //false : 2 is a number
isNaN(2.4); //false : 2.4 is a number
isNaN(2โ€“1); //false : 2โ€“1=1 which is not NaN

//B-Falsy Values
isNaN(0); //false : 0 is a number
isNaN(-0); //false : -0 is a number
isNaN(false); //false : false->0 which is not NaN
isNaN(""); //false : the empty string is converted to 0 which is not NaN
isNaN(NaN); //true : NaN is not equal to any value, including itself
isNaN(undefined); //true : undefined cannot be converted into number, if we try it gives NaN
isNaN(null); //false : null is coerced to a number->0 which is not NaN

//Truthy Values
isNaN(true) //false : true ->1 (a number)
isNaN({}); //true : {} cannot be converted into number, if we try it gives NaN
isNaN([]); //false : [] when it is coerced to a number is becomes 0 (a number)

// Strings
isNaN(""); //false : the empty string is converted to 0 which is not NaN
isNaN(" "); //false : a string with spaces is converted to 0 which is not NaN
isNaN("37"); //false : "37" is converted to the number 37 which is not NaN
isNaN("37.37"); //false : "37.37" is converted to the number 37.37 which is not NaN
isNaN("2โ€“1"); //true : "2โ€“1" is a string
isNaN("37,5"); //true : "37,5" is a string
isNaN("123ABC"); //true : Number("123ABC") is NaN

// Dates
isNaN("14/04/2023"); //true : it is a string
isNaN(new Date()); //false : Date objects can be converted to a number (timestamp)
isNaN(new Date().toString()); //true : the string representation of a Date object cannot be parsed as a numberj

Comparisons:

isNaN(2); //false : 2 (a number)
isNaN("2"); //false : "2" is converted into 2 (a number)

isNaN(2โ€“1); //false : 2โ€“1=1 (a number)
isNaN("2โ€“1"); //true : "2โ€“1" is a string

isNaN(false); //false : false->0 (a number)
isNaN(true); //false : true ->1 (a number)

isNaN(""); //false : the empty string is converted to 0 which is not NaN
isNaN(" "); //false : a string with spaces is converted to 0 which is not NaN

isNaN(NaN); //true : NaN is not equal to any value, including itself
isNaN(undefined); //true : undefined cannot be converted into number, if we try it gives NaN
isNaN(null); //false : null is coerced to a number->0 which is not NaN

isNaN({}); //true : {} cannot be converted into number, if we try it gives NaN
isNaN([]); //false : [] when it is coerced to a number is becomes 0 (a number)

--

--

Muhammad Faraz Ali
Muhammad Faraz Ali

Written by Muhammad Faraz Ali

Strong believer, Highly motivated and a self learner Software Engineer. Tech stack: JavaScript, React, Next and React Native.