JavaScript Random Facts – typeof null is object

Published by Rienz Otiong on

This is my third post for the series “Just so you know” where we are going to unlock deeper knowledge about JavaScript codes.

For those who are new here in this blog, this series talks about things that some of you might know or not. So without further ado. Let’s proceed to our first code.



Null is an object.

To summarize this, null is an assigned value which means nothing. On the other hand undefined is a declared variable but not defined yet. So be careful if you have an if statement that detects whether a value is an object since null is one of them.

While typeof null returns "object", it’s not actually an object, quite silly right? it’s consider as primitive. This is a bug in JavaScript that apparently can’t be fixed because of backward compatibility.

Fun fact typeof [] is also an object.



NaN (Not A Number) is a type of number.

Even though NaN represents a “Not-a-Number” value, its a special value in which results from a operation that has a non-numeric operands. For example 1 * 'a' or Number('abc').



Access more than expected function parameters

Basically we could use the rest parameters which represent an indefinite number of arguments as an array.

Another example using arrow function.



Array reverse() method

As we all know that reverse method return the array in reverse order. However the interesting part is that it reverse also the order of the original array and it returns itself so whatever you do with the assigned variable will also happen to the original array which doesn’t happen most of Array methods like map, reduce or filter.

This is related to the case of references and side effects from a variable mutation.

You could check out my other post Functional Programming where I somewhat mention about mutation and how to avoid it.


Unary plus (+)

Unary + operator converts its operand to Number type. shorthand for NumberparseInt, parseFloat



Conditional Chain (Shorthand Version)

You might do if statement like this.

Which is fine. However you can do it in a shorter and cleaner way using shorthand if-else statement



[+!+[]]+[+[]] is Equals to 10

You can use six different characters to write a functioning code.

See more on JSFuck.



Copying Array Items

It is better to use spread operator when copying the items of an array to avoid unnecessary mutation like the example below.

As you can see. arr1 doesn’t affects the original array which is arr when changing the value as you are making a new copy of array. On the other hand, arr2 is a reference for arr in which changing the value either of the two will affect both of them.



Array To Object

Most likely we search an array item by its id by using find which iterates every item inside the array.

However it will be slower when you have like a thousand items in the array. What you can do first is make the array as object and the ids of the item would be their key so that you can easily access them rather than checking each item that has that particular id. see the example below.



for-loop vs for-in

We often use for-in to iterate object keys but what happen if we use them in arrays.



Deep Object Comparison

Comparing different declared object will return false and only it would return true if its referring to the original object.

To solve this you need to deep search every property on your own.




For more “Just so you know” posts. Just click here


Internet Nerd · January 18, 2019 at 7:50 pm

Would argue that it’s quite useful to mention that accessing function arguments does not work for arrow functions.

    Rienz · January 18, 2019 at 8:55 pm

    Hi, yes of course thanks for the reminder and will add that so others would know. Cheers!

Tom Miller · February 9, 2019 at 5:22 am

Ran ‘for-loop vs for-in’ and don’t get what you say I should

    Rienz · February 12, 2019 at 9:55 am

    I already updated the article for their specific outputs when using them.

Leave a Reply

%d bloggers like this: