JavaScript weird things | Part 1
Prerequisite: foundational knowledge in programming.
NOTE: This article will give a brief introduction to various topics in ES6, but each topic has a lot more details that may be discussed in future articles.
[1] null bug
____________________
If I told you that there's a bug in JavaScript, would you believe? Not a bug in someone's code, a bug in the LANGUAGE ITSELF!
There is a bug in JS that mostly all JS developers know -But I'm now introducing it to beginners so it might be something new for you- which is the null bug.
What is going wrong with null?
If you read in the mdn or any similar documentation about primitive data types, you will notice that null is considered as a primitive data type, right?
However, when you try to know its type by writing typeof null in the console, you will notice something strange!
This is the JS bug I was talking about, it considers null to be an object although it is a primitive data type.
Why does that happen?
The reason is that when JS was created, its creators represented the objects in its lower-level representation in memory as a group of bits that will always start with zero, and they represented the null in memory as a group of bits that are all zeros! That's the reason for this bug.
You might think "Why have the JS developers not come up with a solution for it by now?", and they can do that. But if they do, most of the existing systems today will break, that is why they’re keeping this bug alive.
[2] an array is an object
In contrast to many other programming languages, JS considers the array nothing but a special type of object. And the reason is that it considers its indices to be the properties of the object. This representation might clarify that:
A difference between arrays and objects in JS is array’s elements are ordered, while the object is not.
[3] == Vs ===
If you are familiar with any other languages like Python or PHP, you know that == is a perfect equality operator that we use to determine if 2 values are exactly equal to each other, but in JS, this rule doesn’t exist anymore! Look at this example:
Do you notice what is weird with ==? it does something that causes 1 to equal true, and “” to equal false, this thing is called type coercion, we might write a whole article about that topic but to simplify it, it is something like implicit type casting, the == operator does its best to make the two operands similar to each other. While the === operator is stricter. A best practice is to use === other than == to avoid unexpected behaviors.
As someone with a traditional server-side languages background; these are the three things I found strange when I started studying JS. In the next articles, we'll explore more weird stuff in JS that you might think cannot exist (Like I thought before), stay tuned!
References:
Software Engineer - Backend Developer
8 个月thanks for you effort ??
Software Engineer || ITI Trainee in the Full Stack Web Development Using Python Track
8 个月Awesome ????