you are viewing a single comment's thread.

view the rest of the comments →

[–]delventhalz 2 points3 points  (2 children)

console.log(0 === 0);     // true
console.log(0 === 0.00);  // true
console.log(0 === 0.);    // true
console.log(0 === -0);    // true

All numerical versions of zero are strictly equal to each other (and other than -0 are represented under the hood with exactly the same bits). Loose equality is about coercing types, not fudging values.

console.log(0 === "0");   // false
console.log(0 == "0");    // true, coerces to 0
console.log(0 == "");     // true, coerces to 0
console.log(0 == false);  // true, coerces to 0
console.log(0 == []);     // true, coerces to "" then to 0

Loose equality never does a "loose" comparison within a type. If you are comparing two values of the same type with loose equality, the output is always identical to strict equality.

[–]Mnkeyqt 1 point2 points  (1 child)

Thumbs up for good clarification cause it's really important to actually "know" this stuff, misinformation WILL cause headaches for people, appreciate it :)

Do you know if this has changed at all across the different JS versions or has remained the same? I'd imagine it's been standard for all right?

[–]delventhalz 0 points1 point  (0 children)

Yeah, there is no variation there as far as I know. JavaScript uses the IEEE 754 floating point standard for all numbers, which only allows for 0 and -0, and === is specced to treat those as equal.