JavaScript Reveals Assumptions About Data Types

😄 JavaScript really makes you think twice. 2 + 2 = 4 🙂 "2" + "2" = "22" 🤔 2 + 2 - 2 = 2 😎 "2" + "2" - "2" = 20 🤯 JavaScript doesn’t break logic — it reveals assumptions. ✔ The + operator can concatenate ✔ The - operator forces numbers ✔ Data types matter more than we realize Memes make it entertaining. Understanding fundamentals makes it predictable. JavaScript isn’t confusing. Not knowing the basics is. 😉 Let’s Discuss Your Web Development Needs ✅: https://lnkd.in/dDCyAgxt

  • No alternative text description for this image

JavaScript isn’t inconsistent here — it’s just applying implicit type coercion according to its rules. 😊

So lets try this in python: 2 * '2'

Like
Reply

"2" + "2" - "2" As "2" + "2" - string concatenation going on that's why 22 "22" - "2" - "-" arithmetic subtraction symbol detected that's why 20

When you write "2" + "2" Javascript simply concatenates it i.e. "22". But when you write "2" + "2" - "2" JavaScript Concatenates first then in - "2" arithmetic symbol appears that's why JavaScript Subtract 2 from 22 i.e. 20. That's it.

Like
Reply

Also, "2" + 2 - "2" = 20 "2" - "2" + "2" = "02" "2" - 2 + "2" = "02"

Because, here 2 is not a number, it is string so value we could not find like math.

Exactly 💯 People call these bugs, but this is what makes JavaScript interesting. Once you understand type coercion, things like "2" + "2" or NaN being a number actually become predictable.

"2"+"2" as string concatenation or plus Strings so that 22

Javascript always work from left to right.

See more comments

To view or add a comment, sign in

Explore content categories