javascript

How to Check If A Value Is Not A Number in Javascript


In this article, you will learn how to Check If A Value Is Not A Number in Javascript.

Let’s say you have 5 types of variables.

// 1: A number variable named 'a' with the value 123
var a = 123;

// 2: A string variable named 'b' with the value "codesource"
var b = "codesource";

// 3: A boolean variable named 'c' with the value true
var c = true;

// 4: An undefined variable named 'd' with the value undefined
var d;

// 5: A null variable named 'e' with the value null
var e = null;

In order to check if a variable’s value is a number or not, you can use the isNaN() method.

var a = 123;
var b = “codesource”;
var c = true;
var d;
var e = null;

console.log(isNaN(a));
console.log(isNaN(b));
console.log(isNaN(c));
console.log(isNaN(d));
console.log(isNaN(e));

Note: The word NaN stands for Not A Number. The isNaN method functions by checking whether a value is a legitimate number or not in Javascript. It will return true if the value is not a number. Else, it will return false.


Share on social media

//