Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

price === NaN 永远是个false #2036

Closed
wants to merge 1 commit into from
Closed

Conversation

BarleyBig
Copy link

js中 NaN!==NaN

@luckyadam
Copy link
Member

所以文档标注这个写法是错的

@js-newbee
Copy link
Contributor

那么 price !== NaN 不也永远是 true 么... 感觉文档没毛病

@Garfield550
Copy link
Collaborator

先说观点:我也觉得原文档没毛病,所以这个修改是不必要的。

理由:

  1. MDN 文档关于 NaN 的判断方式已经说得很清楚了:

NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.

  1. 我对于提交者写的 NaN !== NaN 的作用意义不明。

  2. 我个人对 NaN 的理解也不是很清晰,写了以下断言做了测试,发现 NaN !== NaN 没什么意义,断言全部 PASS:

console.assert(NaN === NaN, 'ASSERT: NaN equals NaN'); // Assertion failed: ASSERT: NaN equals NaN
console.assert(Number.NaN === NaN, 'ASSERT: Number.NaN equals NaN'); // Assertion failed: ASSERT: Number.NaN equals NaN
console.assert('NotANumber' === NaN, 'ASSERT: String NotANumber equals NaN'); // Assertion failed: ASSERT: String NotANumber equals NaN
console.assert('1' === NaN, 'ASSERT: String 1 equals NaN'); // Assertion failed: ASSERT: String 1 equals NaN
console.assert(1 === NaN, 'ASSERT: Number 1 equals NaN'); // Assertion failed: ASSERT: Number 1 equals NaN
console.assert(Infinity === NaN, 'ASSERT: Infinity equals NaN'); // Assertion failed: ASSERT: Infinity equals NaN
console.assert(true === NaN, 'ASSERT: Boolean true equals NaN'); // Assertion failed: ASSERT: Boolean true equals NaN
console.assert(false === NaN, 'ASSERT: Boolean false equals NaN'); // Assertion failed: ASSERT: Boolean false equals NaN
console.assert(null === NaN, 'ASSERT: Null equals NaN'); // Assertion failed: ASSERT: Null equals NaN
console.assert(undefined === NaN, 'ASSERT: Undefined equals NaN'); // Assertion failed: ASSERT: Undefined equals NaN

console.assert(NaN !== NaN, 'ASSERT: NaN not equal NaN'); // PASS
console.assert(Number.NaN !== NaN, 'ASSERT: Number.NaN not equal NaN'); // PASS
console.assert('NotANumber' !== NaN, 'ASSERT: String NotANumber not equal NaN'); // PASS
console.assert('1' !== NaN, 'ASSERT: String 1 not equal NaN'); // PASS
console.assert(1 !== NaN, 'ASSERT: Number 1 not equal NaN'); // PASS
console.assert(Infinity !== NaN, 'ASSERT: Infinity not equal NaN'); // PASS
console.assert(true !== NaN, 'ASSERT: Boolean true not equal NaN'); // PASS
console.assert(false !== NaN, 'ASSERT: Boolean false not equal NaN'); // PASS
console.assert(null !== NaN, 'ASSERT: Null not equal NaN'); // PASS
console.assert(undefined !== NaN, 'ASSERT: Undefined not equal NaN'); // PASS

console.assert(isNaN(NaN), 'ASSERT: isNaN(NaN)'); // PASS
console.assert(isNaN(Number.NaN), 'ASSERT: isNaN(Number.NaN)'); // PASS
console.assert(isNaN('NotANumber'), `ASSERT: isNaN('NotANumber')`); // PASS
console.assert(isNaN('1'), `ASSERT: isNaN('1')`); // Assertion failed: ASSERT: isNaN('1')
console.assert(isNaN(1), 'ASSERT: isNaN(1)'); // Assertion failed: ASSERT: isNaN(1)
console.assert(isNaN(Infinity), 'ASSERT: isNaN(Infinity)'); // Assertion failed: ASSERT: isNaN(Infinity)
console.assert(isNaN(true), 'ASSERT: isNaN(true)'); // Assertion failed: ASSERT: isNaN(true)
console.assert(isNaN(false), 'ASSERT: isNaN(false)'); // Assertion failed: ASSERT: isNaN(false)
console.assert(isNaN(null), 'ASSERT: isNaN(null)'); // Assertion failed: ASSERT: isNaN(null)
console.assert(isNaN(undefined), 'ASSERT: isNaN(undefined)'); // PASS

console.assert(Number.isNaN(NaN), 'ASSERT: Number.isNaN(NaN)'); // PASS
console.assert(Number.isNaN(Number.NaN), 'ASSERT: Number.isNaN(Number.NaN)'); // PASS
console.assert(Number.isNaN('NotANumber'), `ASSERT: Number.isNaN('NotANumber')`); // Assertion failed: ASSERT: Number.isNaN('NotANumber')
console.assert(Number.isNaN('1'), `ASSERT: Number.isNaN('1')`); // Assertion failed: ASSERT: Number.isNaN('1')
console.assert(Number.isNaN(1), 'ASSERT: Number.isNaN(1)'); // Assertion failed: ASSERT: Number.isNaN(1)
console.assert(Number.isNaN(Infinity), 'ASSERT: Number.isNaN(Infinity)'); // Assertion failed: ASSERT: Number.isNaN(Infinity)
console.assert(Number.isNaN(true), 'ASSERT: Number.isNaN(true)'); // Assertion failed: ASSERT: Number.isNaN(true)
console.assert(Number.isNaN(false), 'ASSERT: Number.isNaN(false)'); // Assertion failed: ASSERT: Number.isNaN(false)
console.assert(Number.isNaN(null), 'ASSERT: Number.isNaN(null)'); // Assertion failed: ASSERT: Number.isNaN(null)
console.assert(Number.isNaN(undefined), 'ASSERT: Number.isNaN(undefined)'); // Assertion failed: ASSERT: Number.isNaN(undefined)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants