Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JavaScript, in what situation(s) is a === b, but using a or b can give different results?

Tags:

javascript

It may look like if a === b is true, then any operation using either a or b should give the exact same result.

But I do see an exception: 0 === -0 but 1/0 gives Infinity and 1/-0 gives -Infinity.

Are there any other case(s) in JavaScript this can happen?

like image 718
nonopolarity Avatar asked Jan 10 '14 01:01

nonopolarity


Video Answer


1 Answers

No. According to the Strict Equality Comparison Algorithm (EcmaScript §11.9.6) this is the only exception where two different values produce true.

However, a similar pitfall exists for the reverse situation: NaN is an exception where calling the algorithm with the exact same value yields false.

like image 94
Bergi Avatar answered Sep 30 '22 16:09

Bergi