DEV Community

Cover image for Think You Know JavaScript? These 15 Quirks Will Change Your Mind! πŸ’₯

Think You Know JavaScript? These 15 Quirks Will Change Your Mind! πŸ’₯

Jagroop Singh on November 13, 2024

JavaScript is a flexible, quirky language that can sometimes leave you scratching your head. If you've spent any amount of time working with JS, yo...
Collapse
 
moopet profile image
Ben Sinclair

99.1% of these issues people have with JavaScript are caused by trying to compare or manipulate things of different types.

74.8% of these things are common to other programming languages.

I feel like most of the things people find weird about JS are because they're trying to do something that if you stop and think about it for more than a second, well, it doesn't make sense.

Collapse
 
lisaresnick10001 profile image
Lisa Resnick

This is pretty accurate, but one of the main reasons JavaScript is so popular is its "flexible and forgiving nature." People often mix and match different types because the language allows it. For

example:

console.log(1 + "1"); // "11"
console.log(1 - "1"); // 0

This kind of behavior can confuse new developers. But if you approach JavaScript from the perspective of strongly typed languages (like Java or C#), it might seem "weird."

The truth is, if developers understand type conversions and implicit coercion, JavaScript doesn't feel so strange anymore. And as you said, "if you stop and think about it," most of the problems do go away β€” that's the golden rule of programming in any language. πŸ˜„

Collapse
 
jagroop2001 profile image
Jagroop Singh

@moopet ,
I agree !!
It’s true that many JavaScript quirks stem from type coercion and comparison issues, which can often be avoided and not widely used.

Collapse
 
hbthepencil profile image
HB_the_Pencil
  1. I'm gonna guess... typeof NaN returns "number" because it is a placeholder for something that should be a number.
  2. It ought to be 0, but I'm guessing it's actually Infinity because subtracting anything from Infinity is Infinity. (Either that or -Infinity because it subtracts until it cannot subtract any more.)

This is a fun article! I'd love to see more of these!

Collapse
 
jagroop2001 profile image
Jagroop Singh

@hbthepencil , thanks for trying , but the actual answers are:

  1. NaN is technically a special value of the Number in javascript.
  2. When you subtract Infinity from Infinity in JavaScript, you get NaN. This is because the operation is undefinedβ€”subtracting two infinite quantities does not yield a finite number or a meaningful result.
Collapse
 
hbthepencil profile image
HB_the_Pencil

Ah, I was almost there on the first one... Thanks!

Collapse
 
freshcaffeine profile image
Andy Robinson

Heres a fun one that trips up lots of people...

 const christmasDay = new Date(2024, 12, 25);

 const christmasDayString = christmasDay.toLocaleDateString("en-GB", {
   weekday: "long",
   year: "numeric",
   month: "long",
   day: "numeric"
 });
// result: Saturday 25 January 2025
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jagroop2001 profile image
Jagroop Singh

Yes @freshcaffeine ,
Date object are zero-indexed. This means 0 is January, 1 is February, and so on.That's why it cause issue.

Collapse
 
freshcaffeine profile image
Andy Robinson • Edited

yes, but only month is zero-indexed. But not day and year.. which increased confusion more - I've seen some interesting bugs because of this.

Collapse
 
rjbaddeley profile image
Robert Baddeley

The 0.1 + 0.2 and the 0.1*0.1 is not really JavaScript but because floating point math is not precise when it comes down to storing the bits in memory. Many other languages have the same issue. Less a quark of JavaScript and more of a quark of computer science.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Yes , I agree @rjbaddeley

Collapse
 
tomasdevs profile image
Tomas Stveracek

Great article! For those interested, I've also written a similar piece diving into JavaScript quirks: JavaScript Quirks: What You Need to Know. Feel free to check it out! 😊

Collapse
 
jagroop2001 profile image
Jagroop Singh

Great post @tomasdevs !!

Collapse
 
john12 profile image
john

Finally , the old programming language still have bugs, console.log(typeof null); // object which shows no one is perfect even javascript

Collapse
 
jagroop2001 profile image
Jagroop Singh

Image description

Collapse
 
jagroop2001 profile image
Jagroop Singh

Haha, yes, even JavaScript has its quirks!

Collapse
 
paxnw profile image
caga

[1,2,3] + [4,5,6] = "1,2,34,5,6" is asked in my recent interview and I fails to answer it correctly, and after checking the solution I just byheart this and will never forget that.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Yes that's tricky one !!

Collapse
 
ezekiel_77 profile image
Ezekiel

And thats why we use typescript

Collapse
 
jagroop2001 profile image
Jagroop Singh

Exactly @ezekiel_77 , these quirks shows it's important to use typescript in js based projects.

Collapse
 
works profile image
Web

Nice javascript tricky points.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks @works

Collapse
 
lexlohr profile image
Alex Lohr

About #5, you can also use Object.is(NaN, NaN). About #11, parseInt does exactly what is should and returns the integer value of the number. The rest is IEEE754 and type coercion wackyness - and after a few decades of writing JS, those were no longer surprising.

About your tricky questions: NaN is of type number, because it is specified like this in IEEE754 (not only in JS) and Infinity - Infinity yields NaN.

Collapse
 
swape profile image
Alireza Balouch

I was just wondering in what situation you have to compare and empty array with an empty object and what other languages can actually do that? Many of those examples are just bad coding or misunderstood how JS works.

Collapse
 
wizard798 profile image
Wizard

For more informative knowledge, it's good article, bit deep down inside, we all know this doesn't matter