DEV Community

Cover image for JavaScript is Crazy 🀯 - Part 1
Dhairya Shah
Dhairya Shah

Posted on β€’ Edited on β€’ Originally published at codewithsnowbit.hashnode.dev

6

JavaScript is Crazy 🀯 - Part 1

Hello Folks πŸ‘‹

What's up friends, this is SnowBit here. I am a young self-taught and passionate developer and have an intention to become a successful developer.

Today, I am here with a little crazy thing.

So, let's get started.

console.log("5" + 5)
// Output: 55
Enter fullscreen mode Exit fullscreen mode

Here, the output will be 55 as the string is added with a number and that will join the string with the number.

console.log("Hello " + "World")
Enter fullscreen mode Exit fullscreen mode

When adding:

  • Two strings
  • A string and a number

will join with the string.

When the + operator is used with the string is called concatenation operator

Now here comes something crazy

console.log("5" - 5)
Enter fullscreen mode Exit fullscreen mode

Logic of concatenation operator doesn't apply in the case of - operator. Let me explain.

Concatenation operator can join string and add numbers, but - operator can only substract. Therefore, here "5" will get converted to integer and the output will be 0 as 5 - 5 is zero.

Let's go with some other examples,

console.log("8" - 4)
// Output: 4
console.log("10" - 100)
// Output: -90
Enter fullscreen mode Exit fullscreen mode

Thank you for reading, have a nice day!
Your appreciation is my motivation 😊

Cover Photo by Michal Matlon on Unsplash

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay