5 Stupid Things about JavaScript

  1. Objects/arrays of the same value, as long as they are not a single variable, do not equal.

It’s true. You can go to the Console and check right now.

[] === []
false
[] == []
false

The reason this happens is because objects/arrays are stored in memory. The equality symbols === and == check for the same exact instance, with some exceptions. The exceptions do not include objects/arrays with the same value that are not the exact same variable. By “exact same variable”, I mean:

const myVariable = [];myVariable === myVariable
true
myVariable == myVariable
true

The workaround for this is to use JSON.stringify:

JSON.stringify([]) === JSON.stringify([])
true
JSON.stringify([]) == JSON.stringify([])
true

2. NaN does not equal NaN.

Yes. NaN === NaN (and even NaN == NaN) returns false. You must use isNaN(NaN).

3. Arrays are actually objects.

typeof([]) returns object.

4. Dividing by zero gives different results.

0 / 0
NaN
1 / 0
Infinity
-1 / 0
-Infinity

5. JavaScript automatically inserts semicolons.

function a() {
return {
b: "c"
};
}
function b() {
return
{
c: "d"
};
}
a()
Object: { b: "c" }
b()
undefined
// Yet these are the same...try {
// ...
} catch (err) {
// ...
}
// and
try {
// ...
}
catch (err) {
// ...
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store