高级前端js【Q622】Number.isNaN 与 globalThis.isNaN 有何区别

Number.isNaN 与 globalThis.isNaN 有何区别

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 640

Author 回答者: shfshanyue

Number.isNaN(NaN);
isNaN(NaN);
 
Number.isNaN("NaN");
isNaN("NaN");

Author 回答者: nmsn

Number.isNaN(NaN);
isNaN(NaN);
 
Number.isNaN("NaN");
isNaN("NaN");

试了下 Number.isNaN('NaN')false,其他都是 true

Author 回答者: HydratedPig

还真没注意过,看了下 MDN,上面解释已经很明示了

  • isNaN:

    Return Value: true if the given value is NaN after being converted to a number; otherwise, false.

  • Number.isNaN:

    Return value: The boolean value true if the given value is a number with value NaN. Otherwise, false.

globalThis.isNaN 会对参数进行强制转换后判断是不是 NaN,而 Number.isNaN 不会对输入的参数进行强制转换