JavaScript isNaN function and usage

In JavaScript we have a function isNan (Not a number) to check whether the value is a number or not. It takes only one parameter, value and returns true or false, we will check here some values and conditions to understand it better.

It supports in all browsers.

isNaN(123);   // false -- 123 is a number

isNaN(1.23);   // false -- 1.23 is a number

isNaN(12 - 3);   // false -- 12-3 = 9 is a number    

isNaN(0);   // false -- 0 is a number

isNaN('3S');   // true -- 3S is not a number  but string so not a number

isNaN('abcd');   // true -- abcd is a string so not a number

isNaN('01/02/20013');   // true -- 01/02/20013 is a date so not a number

isNaN('01:02:03');   // true -- 01:02:03 is time so not a number

var a;  var b; // value not defined
isNaN(a/b);   // true 

isNaN(a/b);   // true -- because b is not defined

isNaN(a/b);   // false -- a/b=10/5 = 2 is a number

var a= [1, 2, 3,4,5,];
var b; // not initialized
for(i=0; i<a.length; i++)
   b = b + a[i]
isNaN(b); // true -- because b is undefined.

b was not initialize before the loop start, so b is undefined so undefined + any value = undefined

Jason Olivera
  • javascript
By Jason Olivera On 30 Mar, 13  Viewed: 535

Other blogs you may like

A handy tutorial on creating Page Load Progress Bar using Pace.js

A website isn't complete without the inclusion of all the right elements. Progress bar is one of the key components of every website. It informs the user about the total waiting time for a particular web page or a specific task to load. Apart from adding value to the website, the progress bar is... By Celin Smith   On 18 Feb 2015  Viewed: 4,970

JavaScript Array: Create, Aceess and Methods

ArrayList and JavaScript, How to declare and store an ArrayList inside JavaScript, create an array in javascript, add item to javascript array, delete an item in an array in javascript, find an item in an array in javascript, these are the very basic question which most of the new developer... By Hamden   On 30 Dec 2012  Viewed: 843

Use JavaScript to hide and show text and graphics

You can show and hide the content to get attention of the user on a web page. it's a very useful thing for web designer and web developers. You can use JavaScript and CSS to achieve it. I am going to show some examples of it. There are many libraries available on internet having the feature to... By Hamden   On 30 Dec 2012  Viewed: 781

JavaScript getAttribute() Method

In JavaScript getAttribute() method is used to get the value of an attribute in an object. **Syntax:** object.getAttribute(attributename) **Parameter:** It takes only one parameter *attributename* **Example:** Suppose you have an anchor tag with following attributes <a... By Alicia Gonzalez   On 02 Dec 2012  Viewed: 704

How to retrieve data from database using JavaScript in

I was checking how to validate a user email from database without post back, using JavaScript. After some search and RND found the way to call the web service method with the help of Ajax and JavaScript. Let’s say we have a registration page on which we accept email from the user and we expect... By Ali Adravi   On 24 Jun 2012  Viewed: 14,670