当其中一个操作数为 NaN 时,哪个算术运算符不会产生 NaN 结果?

Which arithmetic operator can produce not NaN result when one of operands will be NaN?

纯属兴趣。我们知道 NaN 正在沿着边计算传播。

1 + NaN => NaN

有没有运营商可以阻止这种传播。我的意思是:

<operator> NaN => number

NaN <operator> <operand> => number

<operand> <operator> NaN => number

NaN => 数字

实际上在 Ecmascript 中定义了数字的 (**) 运算符。当第二个操作数为 +0 或 -0 时有一个特殊情况。

https://tc39.es/ecma262/multipage/ecmascript-data-types-and-values.html#sec-numeric-types-number-exponentiate

实际上:

console.log(NaN ** 0) // will log 1

Javascript 有一个检测 NaN 的 built-in 方法,但您需要为每个操作数检查这个“可能的数字”。

let possibleNumber = "4f";

console.log(isNaN(Number(possibleNumber)));

possibleNumber = "44";

console.log(isNaN(Number(possibleNumber)));

输出:

true
false

你可以做一些功能来检查和return一些你想要的数字

function checkIfIsNaN(number,numIfTrue,numIfFalse){
    if(isNaN(Number(number))){ return numIfTrue} else{return numIfFalse};
}

console.log(checkIfIsNaN("44",4,10));