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.
Output :
-----------------------
Output :
-----------------------
This is all about Checking if variable is a number in JavaScript. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.
- 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."
Using isNaN() function:
In this example, we have used isNaN() function, and verifying the variable is number or not in javascript.// In this variable we are storing number... var num1 = 536; // verifying by using isNaN function operator if(isNaN(num1)){ console.log(num1 + " is not a number."); }else{ console.log(num1 + " is a number."); }
Output :
-----------------------
> "536 is a number."
This is all about Checking if variable is a number in JavaScript. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.
No comments:
Post a Comment