Introduction to JavaScript NaN property

 JavaScript NaN is a special non-numeric value. To put it plainly, it is "not a representative of a number", NaN = Not a Number. So NaN is used to do mathematical operations in JavaScript or to do calculations with other numbers, and you will get NaN. In other words, NaN is not equal to any number (Number), nor is it equal to itself. It sounds a bit abstract. How to judge a parameter Is it NaN? It can be detected through the JavaScript isNaN function .


JavaScript NaN case study
<script type='text/javascript'>
var StrA="100";
var StrB="這是字串";

document.write(Number(StrA)+ "<br>"); // Output 100
document.write(isNaN(StrA)+ "<p>"); // Output false

document.write(Number(StrB)+ "<br>"); // Output NaN
document.write(isNaN(StrB)+ "<p>"); // Output true
</script>
At the beginning of the example, we first prepared two variables, namely StrA and StrB, and then prepared two sets of grammars for detecting parameters below. Number first checks whether StrA is a number, and finds that it outputs 100 smoothly, and then uses isNaN to check whether StrA is For NaN, false is output smoothly. The same method is used to detect StrB to obtain the detection result.

Post a Comment

0 Comments