DEV Community

Cover image for Do you know JavaScript?
Leonardo Montini for This is Learning

Posted on • Originally published at leonardomontini.dev

Do you know JavaScript?

Do you know Javascript? The language we all love has some weird and sometimes unexpected behaviours.

It is a good idea to know them, mostly to avoid unexpected bugs and unpleasant surprises.

Do you want an example?



const a = 0.1;
const b = 0.2;
const c = 0.3;

console.log(a + b === c); // false


Enter fullscreen mode Exit fullscreen mode

The output here is false. The sum 0.1 + 0.2 is not equal to 0.3.

Why does this happen? It's because of how floating point numbers are represented in JavaScript.

One more cool example:



"2" + "3" === "23"; // true


Enter fullscreen mode Exit fullscreen mode

This was expected, right? We're concatenating two strings. But what if we replace + with *?



"2" * "3" === 6; // true


Enter fullscreen mode Exit fullscreen mode

In this case, the * operator tried to convert the two strings in numbers, that's why we got 6.

The minigames

I've collected 12 situations like these and I will walk you through them in this video. I will also use the node REPL to further expand on some of them when the output is particularly weird.


This video format is something I've had in mind for a while and I wanted to give it a try. To be honest I'm not too satisfied with the outcome, but I wanted to share it anyway to collect feedback and make even better versions in the future. I'd like to go with multiline code snippets and decide if keep the explanation or make it faster and more entertaining by skipping them. What do you think?

With that said, I hope you enjoyed it and learned something new! Please let me know if you have suggestions on how to improve the video, or if you have any other feedback in general.


Thanks for reading this article, I hope you found it interesting!

I recently launched my Discord server to talk about Open Source and Web Development, feel free to join: https://discord.gg/bqwyEa6We6

Do you like my content? You might consider subscribing to my YouTube channel! It means a lot to me ❤️
You can find it here:
YouTube

Feel free to follow me to get notified when new articles are out ;)

Top comments (2)

Collapse
 
bokomoko profile image
João Eurico "Bokomoko" Lima

The issue of 0.2 + 0.1 not being exactly equal to 0.3 is not particular to JavaScript. Almost all languages that provide floating point arithmetic has this problem.

That's why it's much better to work only with integer/fixed point numbers whenever possible. For instance, instead of storing monetary values as float with 2 decimal points, prefer to store cents (hundreds) and work up the decimal points on the formating of the values. The same applies for other units, like centimeters instead of meters, or grams instead of kilograms. The idea is to use the least significant unit that can be represented by an integer and work the way up to greater units.

Collapse
 
balastrong profile image
Leonardo Montini

That's great advice, thanks for sharing :)