[英]what is the reserved keyword for NaN in javascript?
if i want to test the result of an expression and the function would return NaN how would i check that? 如果我想测试表达式的结果,函数将返回NaN我将如何检查?
examples: 例子:
$('amount').value.toInt()!='NaN'
^ does not work and i assume that the returned value is not a string, ^不起作用,我假设返回的值不是字符串,
$('amount').value.toInt()!=NaN
^ doesnt seem to work either and this one seems obvious ^似乎也没有工作,这一个似乎很明显
so how do i check wether the returned value is not a number? 那么如何检查返回值是不是一个数字?
The NaN value is defined to be unequal to everything, including itself. NaN值被定义为不等于所有东西,包括它自身。 Test if a value is NaN with the
isNaN()
function, appropriately enough. 使用
isNaN()
函数测试值是否为NaN,足够恰当。 (ECMAScript 6 adds a Number.isNan()
function with different semantics for non-number arguments, but it's not supported in all browsers yet as of 2015). (ECMAScript 6为非数字参数添加了一个具有不同语义的
Number.isNan()
函数,但截至2015年,它并未在所有浏览器中都受支持)。
There are two built-in properties available with a NaN value: the global NaN
property (ie window.NaN
in browsers), and Number.NaN
. 有两个内置属性可用,具有NaN值:全局
NaN
属性(即浏览器中的window.NaN
)和Number.NaN
。 It is not a language keyword. 它不是语言关键字。 In older browsers, the
NaN
property could be overwritten, with potentially confusing results, but with the ECMAScript 5 standard it was made non-writable . 在较旧的浏览器中,
NaN
属性可能会被覆盖,结果可能会令人困惑,但是使用ECMAScript 5标准时,它是不可写的 。
the best way to check the result of numeric operation against NaN is to aproach this way , example: 检查针对NaN的数值运算结果的最佳方法是以这种方式实现,例如:
var x = 0;
var y = 0;
var result = x/y; // we know that result will be NaN value
// to test if result holds a NaN value we should use the following code :
if(result !=result){
console.log('this is an NaN value');
}
and it's done. 它已经完成了。
the trick is that NaN can't be compared to any other value even with it self(NaN !=NaN is always true so we can take advantage of this and compare result against itself) 诀窍是NaN无法与任何其他值进行比较,即使是自我(NaN!= NaN总是如此,所以我们可以利用这个并将结果与自身进行比较)
this is JavaScript(a good and bizarre language) 这是JavaScript(一种优秀而奇异的语言)
Equality operator (== and ===) cannot be used to test a value against NaN. 等式运算符(==和===)不能用于测试针对NaN的值。 Use Number.isNaN() or isNaN() instead.
请改用Number.isNaN()或isNaN()。
NaN === NaN; // false
Number.NaN === NaN; // false
isNaN(NaN); // true
isNaN(Number.NaN); // true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.