JavaScript weird things | Part 1
Photo credits to https://snipcart.com/blog/why-javascript-benefits

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:


objects vs arrays

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:

== vs ===

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:

https://frontendmasters.com/courses/javascript-first-steps/

https://developer.mozilla.org/en-US/

Shaimaa Fikry

Software Engineer - Backend Developer

8 个月

thanks for you effort ??

saraa talat

Software Engineer || ITI Trainee in the Full Stack Web Development Using Python Track

8 个月

Awesome ????

要查看或添加评论,请登录

Aya Ragab的更多文章

  • ALX SE — How I Survived In This Journey of Ups and Downs

    ALX SE — How I Survived In This Journey of Ups and Downs

    Introduction: My Start with ALX SE I was a fresh student in my faculty—Computer science faculty—when I joined the ALX…

    19 条评论
  • JavaScript weird things | Part 2

    JavaScript weird things | Part 2

    Prerequisite: foundational knowledge in programming. NOTE: This article will give a brief introduction to various…

    1 条评论
  • Mutability and Immutability of Variables and their values in JS

    Mutability and Immutability of Variables and their values in JS

    Prerequisites: A few programming basics (Variables, data types, and arrays) IMPORTANT NOTE: When taking a JS course and…

    1 条评论
  • JavaScript landscape | beginner guide

    JavaScript landscape | beginner guide

    Prerequisites for this article: none. Have you ever felt overwhelmed by the JavaScript jargon? Or asked yourself, "What…

    2 条评论
  • Objects nature in JavaScript (1) | for beginners

    Objects nature in JavaScript (1) | for beginners

    This is the first article I wrote in my newsletter "Intro to JavaScript" and I want to point out that having a strong…

    1 条评论