JavaScript -: The Data Types are weird

Weird parts of JavaScript which every developer stumbles upon

Anikettyagi
3 min readJan 2, 2021
photo by Tj Holowaychuk on Unsplash

How these problem originated?

Marc Andreessen, founder of Netscape Communications, had a vision for the web to make it more interactive. And thus, the vision of Mocha came into existence, a simple scripting language of the web. Which would help the world to make websites more user-friendly and interactive, as it is now. So Netscape hired Brendan Eich, to develop a “Scheme for the Web”. There were lots of talks to make Java as the default language of the Web. But it was simply too BIG. There was a lot of internal pressure to pick one language as soon as possible. Python, Tcl, Scheme itself were all possible candidates. So Eich had to work fast.

Eich designed a language that was similar to Java in its syntax. And he created the first version of JS ( called Mocha at that time ) in just 10 days. Yes in 10 days!, so it created many loopholes in the language. We will discuss some of those “loopholes” today. So open your CONSOLE and lets try some things.

Null and Undefined

We know about the null and the undefined data types in JS. Lets try something now,

Type typeof(undefined) in your console, you will receive undefined as expected, cool. Now try typeof(null) ,What? object . That does not make sense right, It should have been null .

Another, the values of undefined and null are same. Wait. What? How?

The Double Equality operator “==” will perform coercion when comparing two things. That means the “==” will only check for their values not caring about the Data Type. Lets try undefined == null we get true meaning the values of undefined and null are same only their Data Types are different. WEIRD!

Objects and Arrays

No Two Objects or Arrays are same, even with same value. Lets see,

var a =[1]
var b =[1]
a == b

gives false and it is same for the Objects. But if we assign a variable with other variable which contains Objects or an Array would result in true, eg

var a =[1]
var b =a
a == b

gives true .

Now, lets try something different

var a =1
var b =a
a=2
console.log(b)

This will print 1 as expected because we are assigning b to a which is 1 any change to a will not reflect in b , acceptable right? lets see this with Objects

var a ={"hey":1}
var b =a
a.hey = 2
console.log(b.hey)

This will print 2 , the same is with Arrays. The reason behind this is quite simple, while declaring Objects and Arrays, the variable acts as a reference to the data, so any changes made to the variable pointing to the data will be reflected on all the variables. But why some data types works in some way while the others in completely different way?

BONUS!

Bonus for being a great audience, again with the null Data Type. Listen! null is neither equal to 0 nor greater than 0 but apparently ≥ 0 . NOOOOO!!! LOL!

null == 0 //false
null > 0 //false
null >= 0 //true

I would be adding more to this Weird but interesting list. Please correct me where ever I made a mistake in this blog, in the comment section below. Your views means a lot to me.

--

--