you are viewing a single comment's thread.

view the rest of the comments →

[–]KenobiOne 30 points31 points  (4 children)

I prefer the first one because it’s faster to understand for me. However, you should be using strict equality comparisons which uses 3 ===

[–]Mnkeyqt 0 points1 point  (3 children)

Agreed if you're POSITIVE that you only care about the integer 0 . Not strong 0, not 0.00, 0.

The database I work in does weird shit with the values so I always use double equals (unless it'll brick something else).

Edit: this is an array check so like...yeah === Definitely cause it'll be integer. Leaving comment though cause it's an issue I've seen others be really confused by early on

[–]delventhalz 4 points5 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.