|
| |
Checking number using isNaN function |
JavaScript has one important function isNaN() to check any data is number or string. This function isNaN() is remembered as short form is Is Not a Number. This function returns true if the data is string and returns false if the data is a number. Here is one simple example to check one string. We will use one if else condition check to display a message accordingly. Here is the code.
var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}
We will integrate this function to one JavaScript prompt. Here we will ask the user to enter a number. The value entered by user we will check by isNan function and display message accordingly. Here is the code.
var my_string = prompt("Please enter a number","");
document.write(my_string)
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}
| |
| Subscribe |
|
Submit your email address and receive
article and product notifications. Your email is safe with us.
|
|
|
|