Lately whenever I’ve had some idle time, whether that be waiting for my Windows box to start up, or waiting for my project to build, I’ve been reading MDN’s JavaScript docs. As someone who’s never formally studied JavaScript, it has plenty of useful tidbits that I may not gather from just building applications.
null
and undefined
are considered data types in JavaScript
This probably comes in more useful for random trivia and interviews than anything else.
But unlike Java, where
null
is simply a possible value,
null
is one of
6 primitive data types in JavaScript
(Boolean, null, undefined, Number, String, and Symbol).
Don't use for ... in
to loop through arrays
for ... in
loops through an object's
property values,
not array values.
So if you have an array with extra user-defined properties,
looping using
for ... in
would list all its existing elements
and the user-defined properties.
For arrays, it's better to use
for ... of
.
Luckily, I've never run into this issue.
I always use the other alternative, forEach
.
Primitive parameters are passed by value, non-primitive values are passed by reference
I had already figured this out through experience, but it's always nice to have confirmation. This is unlike C++, where you get to choose the manner a variable is passed.
Rest parameters
Rest parameters allow the representation of an indefinite number of arguments as an array. I have never had to use this in JavaScript, but it does remind of a similar idea in Java, varargs. I'm pretty excited to find a need for this concept in my projects.
Anonymous functions
Learning TypeScript and ES2016 before anything else, I have never had the need to use the long hand for anonymous functions. Instead, I always just use fat arrows. But reviewing its syntax is always helpful, especially for reading other people's code.
Maps
As a Python lover, I used maps heavily when I first started using JavaScript. However, I only learned in the documentation that map keys don't need to be strings, and map iteration occurs in the order of insertion.
Sets
I, unfortunately, was not aware of the existence of Sets. They can be compared to Arrays, but only contain unique values. And for cases where relevant, it may be better to use them because they can check element existence faster, delete objects by value (instead of index), and can store NaNs.
Note: I was also not aware that Arrays can't store NaNs properly.