- isNaN() – Stands for “is Not a Number”, if a variable is not a number, it returns true, else return false.
- typeof – If a variable is a number, it will return a string named “number”.
Using isNaN() Function
let num = 50;
if(isNaN(num)){
console.log(num + " is not a number");
}else{
console.log(num + " is a number");
}
let str = "javaguides";
if(isNaN(str)){
console.log(str + " is not a number");
}else{
console.log(str + " is a number");
}
50 is a number
javaguides is not a number
Using typeof Operator
let num = 50;
if(typeof num == 'number'){
console.log(num + " is a number");
}else{
console.log(num + " is not a number");
}
50 is a number
Complete Example with Output
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment