Home > Web Front-end > JS Tutorial > body text

What is the difference between isNaN and Number.isNaN in js

青灯夜游
Release: 2020-11-17 18:18:07
Original
2670 people have browsed it

Difference: Number.isNaN does not have type conversion behavior, and isNaN will try to convert the parameter into Number type. isNaN only determines whether the incoming parameter can be converted into a number, and does not strictly determine whether it is equal to NaN; while Number.isNaN determines whether the incoming parameter is strictly equal to NaN.

What is the difference between isNaN and Number.isNaN in js

Recommended tutorial: "JavaScript Video Tutorial"

## isNaN

When we pass a parameter to isNaN, its original intention is to try to convert the parameter into Number type through the Number() method. If it succeeds, it returns false. If it fails, it returns true.

So isNaN only determines whether the incoming parameter can be converted into a number, and does not strictly determine whether it is equal to NaN.

Example:

Number('测试')
Copy after login
Because "test" was not successfully converted into Number type, the following code outputs true

console.log(isNaN('测试')) //true
Copy after login

Number. isNaN

# Determines whether the incoming parameter is strictly equal to NaN (that is, ===).

So under what circumstances is Number.isNaN generally used?

When two variables are operated on, we can use Number.isNaN to determine whether its value is NaN

console.log(Number.isNaN(1/'测试')); //输出true
Copy after login

The difference between the two

The biggest difference between Number.isNaN and isNaN is that Number.isNaN does not have type conversion behavior.

console.log(isNaN('测试')) //true
console.log(Number.isNaN('测试')) //false
Copy after login
In the above code, the string "test" is passed in, but why are the results different? The reason is:

isNaN tries to convert the string "test" into the Number type through the Number method, but the conversion fails because the result of Number('test') is NaN, so it finally returns true.

The Number.isNaN method only strictly determines whether the incoming parameters are all equal to NaN ('test' === NaN). Of course, the strings are not all equal to NaN, so it outputs false.

For more programming-related knowledge, please visit:

Programming Teaching! !

The above is the detailed content of What is the difference between isNaN and Number.isNaN in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!