Play with JavaScript Number

Some scenarios

Scenario 1

var x = Number.MAX_VALUE; // 1.7976931348623157e+308  
var y = x + 1; // Add one to MAX_VALUE

console.log(x===y); // true  

Scenario 2

var x = Number.MAX_VALUE; // 1.7976931348623157e+308  
var y = x - 1; // Subtract one from MAX_VALUE

console.log(x===y); // true  

Scenario 3

var x = Number.MAX_SAFE_INTEGER; // 9007199254740991  
var y = x + 1; // y = 9007199254740992  
// x is MAX already, but y is even MAXer!
console.log(x===y); // false  

Scenario 4

var x = Number.MAX_SAFE_INTEGER + 1; // 9007199254740992  
var y = x + 1; // y = 9007199254740992  
// x is MAXer already, y cannot be MAXerer!
console.log(x===y); // true  

Are you confused yet? You've seen the issues? No? Scroll up... read again! You want a pen? Come to my desk.

Want an explanation? Read the DOC. The root cause is deep in the memory layout of JavaScript Number Type.

s × m × 2e

Conclusion

Keep these scenarios in your notes, it is likely that interviewers ask you these edge cases. Anyways, we should be aware of false assumptions when applying a programming language - the spec should always be read. You have been warned - stop scratching your head when x === x+1.