Give sense to nonsense series: weird JS explained

Alessandro Merola
salt&pepper
Published in
3 min readApr 28, 2021

“Why?! Why are you giving me this output?! That makes no sense!”

If you’ve worked with Javascript, I bet you can relate to what’s written above. If not, trust me, you will!

Sometimes JS has very, let’s call them, weird behaviors, almost nonsensical, but there’s a reason for all of them! If not PEBCAK then let me try to give sense to nonsense.

COMPARING OBJECTS

Let’s start with the most common “issue” that I came across while mentoring future JS developers.

If the objects are the same, then why is the output false in both cases?
Although it might make no sense at first glance, the reason is pretty simple: Reference. The two objects, o1 and o2, only contain a reference to a location in memory. not the value of the object.

Let me be more specific and provide you with another example:

Here we are comparing primitives. For any primitive, memory is allocated and the value of the primitive is stored in that memory location, so we are comparing the actual value of the two.

In other words:

When comparing o1 and o2, the locations are separate, so the references are different, and the comparison fails. When comparing a and b, we are not comparing the values of the objects, but the references to their memory locations.

FLOATING POINT ARITHMETICS

Let’s move to another funny JS behavior related to comparisons:

Wait, wait, wait. We just looked at the comparison with primitives and now this?
Yes! And it actually works by the book! Have you ever heard of IEEE754?

IEEE — Institute of Electrical and Electronics Engineers, pronounced “Eye-triple-E” — is the largest professional association that develops, defines, and reviews electronic and computer science standards.

IEEE754 is a technical standard for floating-point arithmetic addressing problems concerning the floating-point implementation.

In our case, the two numbers are an approximation of their true value. In fact:

If this tickles your curiosity, here are some useful links to dig deeper into the problem:

  1. Is floating point math broken?
  2. Floating-point math

SILENT CAST

Talking about numbers, I have a nice example of something that tricked me once.

You might say: “That’s an easy one! It silently casts the number to octal because it starts with 0!”. True! But….
8 is not part of the octal system, so 018 is invalid. What happens behind the scene is that the number is going to be converted into a decimal. Hence, 018–017 is in fact equivalent to the decimal expression 18–15, because 017 is octal but 018 is decimal.

I think I’ve given you a lot to think about and a lot of info to process. But stay tuned, because more sense is going to be given to JS’s quirkiness!

FINAL THOUGHTS

Although JS has many weird behaviors, that sometimes give us a headache, once you learn how to navigate them or approach them everything falls into place.
Do you have any interesting or weird behavior that you encountered during your coding time?

--

--