# YOU DON'T KNOW JS YET

## THE POWER OF JS SUBSTRACTION OPERATOR

By Shubham RaiPublished 11 months ago 3 min read
Photo by Jackson Sophat on Unsplash

console.log("1" + 4 - "7") //what do you think? what the ans is? error?

No Worries, It’s a Common Guess! 😄🎲 Many might assume the answer is “7” as a string or error , etc and there’s absolutely no shame in it. Before discovering this concept, my answer was the same too! Let’s dive in and unveil the truth together! 🚀💡

And I promise you before you end up reading this article you will be CONFIDENT on any such question which have subtraction operator.

Get ready for a mind-blowing revelation!

JavaScript, the master of dynamic typing, has a surprising trick that will leave you in awe. When it encounters a string during subtraction, it unleashes its magic and performs a special secret conversion.

Ummm…? Special secret conversion?

Yes, you read that right! JavaScript secretly transforms strings into numbers! 🧙‍♂️ And here’s the trick: the mighty unary plus operator (+) comes to the rescue. Before your very eyes, the string "7" is gracefully transmuted into the numeric value 7 It's pure magic! 🌈🦄

Here is what happening in the code :

console.log("1" + 4 - "7") //step 1 "1" + 4 -> "14"

//step 2 "14" - "7" -> 7 Here type conversion takes place (:

Curious minds might wonder why the addition operator doesn’t trigger this type conversion. If you’ve had this thoughtful question while reading, my friend, the answer awaits you!

I am javascript. No this is not your answer but you might have heard this term concatenation ? right and we do that using (+) operator.

(+) operator does type conversion only when it is used correctly :

let a = "7"

console.log(+a) //pause and think of the output

Ladies and gentlemen, brace yourselves for the drum roll… and the output is… ta-da! It’s the magical number 7, not in strings! Yes, both subtraction and addition indeed perform type conversions, but they work their enchantment in different ways.

Great! It’s time for an exciting assignment! Get ready to dive into the world of JavaScript and put your skills to the test.

const numStr = "123";

const NumStr = "8";

const result1 = +numStr - 10;

const result2 = NumStr - 10;

console.log(result1);

console.log(result2);

Give it a try! Guess what the result will be before running the code. If your guess is correct, congratulations, you’ve grasped the concept! If not, no worries, just take a quick look back at the article for a helpful reminder! (:

1. **Implicit Type Conversion**: When the subtraction operator is used with different data types (e.g., string and number), JavaScript automatically converts the operands into numbers before performing the subtraction.

2. **NaN Result**: If any of the operands cannot be converted into a valid number, the result of the subtraction will be NaN (Not-a-Number).

3. **Order of Operations**: The subtraction operator follows the standard mathematical order of operations (PEMDAS/BODMAS), meaning expressions inside parentheses are evaluated first.

4. **Unary Negation**: The subtraction operator can also be used as a unary operator to negate a numeric value. For example, `-5` will result in `-5`.

5. **Precision Limitation**: JavaScript uses the IEEE 754 double-precision format to represent numbers, which can lead to precision issues in some cases when performing subtraction operations with very large or very small numbers.

Unlock the Gateway to JavaScript Wizardry! 🔮✨ Connect with me on LinkDn for a journey filled with mind-bending JavaScript concepts. 🚀🧙‍♂️ Unleash your coding potential, and together, we’ll conquer the realm of JavaScript mysteries! Don’t miss this magical opportunity! 💫😊

liststudentinterviewhigh schoolcoursescollege

### Shubham Rai

AI & Web Enthusiast | JavaScript Pro 💻 | Passionate about AI advancements, exploring the web's wonders, and crafting magic with JavaScript. Let's dive into tech together! 🚀💡

## Enjoyed the story? .css-tur75i-Box{display:inline-block;}@media (min-width:30em){.css-tur75i-Box{display:inline;}}Support the Creator.

Subscribe for free to receive all their stories in your feed. You could also pledge your support or give them a one-off tip, letting them know you appreciate their work.

How does it work?

There are no comments for this story

Be the first to respond and start the conversation.

Written by Shubham Rai