Welcome to my little node on the network! My name is Petar and you can follow along while I try to figure out how computers work.

Recent notes

Comparisons in Javascript

So what’s the difference between == and === in Javascript? Nothing at all if the types on both ends are the same!

If they are different though, == will try to coerce, before compare and this is where a lot of confusion comes from.

42 == "42"; // true, "42" will be converted to number before comparison
1 == true; // true, because true will be converted to 1 before comparison

This also applies to relation comparisons like < and even <=.

There is no way to avoid coercion with relational comparisons. These are the only ones which are valid:

  • < (less than)
  • > (greater than)
  • <= (less than or equal to)
  • >= (greater than or equal to)
  • == (equality with coercion)
  • === (strict equality, no coercion)

Permalink

Recent Posts

Running to think

For a few years now, I've been partaking in the age-old ritual of running. I began my journey like many others, swathed in quality athletic wear and armed with a meticulous training schedule...

Read more

Recent reads