Check If Variable Is A Number In JavaScript

skptricks

skptricks

Posted on December 2, 2018

Check If Variable Is A Number In JavaScript

Post Link : Check If Variable Is A Number In JavaScript

This tutorial explains how to check variable is a number in javascript. Lets see the below example, where we have used typeof operator and isNaN() function to verify the variable is number or not.
typeof – If variable is a number, it will returns a string named "number".
isNaN() – Stands for "is Not a Number", if variable is not a number, it return true, else return false.

Using typeof operator :
In this example, we have used typeof operator, and verifying the variable is number or not in javscript.
// In this variable we are storing number...
var num1 = 758;

// verifying by using typeof operator
if(typeof num1 == 'number'){
console.log(num1 + " is a number.");
}else{
console.log(num1 + " is not a number.");
}

Output :

"758 is a number."

💖 💪 🙅 🚩
skptricks
skptricks

Posted on December 2, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related