+ 9

JavaScript String Manager

Why in JavaScript "11" + 1 = "111" And "11" - 1 = 10

14th Sep 2019, 6:40 PM
Qudusayo
Qudusayo - avatar
15 Answers
+ 9
"11"- 1 = 10 JavaScript sees that "11" is a string and it sees something is being subtracted from it. It says to itself, “Huh. Something is being subtracted from a string. Does that make sense to me? What do I do when something’s subtracted from a string?” It doesn’t have a rule for that! So it says, “Well, that second argument, the 1, is a number. What if I turn the first argument, the "11", into a number. Can I do that?” It turns out that, yes, it can turn the string "11"into a number. So then it asks, “Does subtracting one number from another make sense? Do I have a rule for subtracting one number from another?” It turns out that, yes, it does know how to subtract one number from another. So it converts the string, "11", to the number, 11, subtracts 1 from it, and displays the result, 10.
14th Sep 2019, 6:46 PM
Qudusayo
Qudusayo - avatar
+ 5
Use parseInt(string).
14th Sep 2019, 6:53 PM
Filipe ClĂĄudio
Filipe ClĂĄudio - avatar
+ 5
Filipe ClĂĄudio I don't ask how to solve but I asked why I know JavaScript
14th Sep 2019, 6:59 PM
Qudusayo
Qudusayo - avatar
+ 5
let a = []; a[0] = 10; console.log(a+1); // result 101 let b = []; b[0] = 10; console.log(parseInt(b)+1) // result 11
14th Sep 2019, 6:59 PM
Filipe ClĂĄudio
Filipe ClĂĄudio - avatar
+ 5
Ok sorry, I just wanted to help
14th Sep 2019, 7:01 PM
Filipe ClĂĄudio
Filipe ClĂĄudio - avatar
+ 5
Anton Böhler There Must be a reason
14th Sep 2019, 7:08 PM
Qudusayo
Qudusayo - avatar
+ 5
It takes place due to coercion... In first case the number 1 is coerced to string and hence displayed as 111 Meanwhile the subtraction operator coerces the 11 to number so 10 https://hackernoon.com/understanding-js-coercion-ff5684475bfc
15th Sep 2019, 5:03 AM
$hardul B
$hardul B - avatar
+ 5
"11"+1 will convert 1 to string to be "111" "11"-1 will convert 11 to number to be 10 And with mult and div same sub...
15th Sep 2019, 4:12 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 4
you've made a little fault, its: "11" + 1 = "111" "11" - 1 = 10
14th Sep 2019, 6:50 PM
Anton Böhler
Anton Böhler - avatar
14th Sep 2019, 6:53 PM
Qudusayo
Qudusayo - avatar
+ 4
Qudusayo probably to avoid runtime errors and also to shorten code đŸ€·â€â™‚ïž
14th Sep 2019, 7:33 PM
Anton Böhler
Anton Böhler - avatar
+ 3
Qudusayo it's just the way JavaScript handles such operations. đŸ€·â€â™‚ïž
14th Sep 2019, 7:04 PM
Anton Böhler
Anton Böhler - avatar
+ 3
if there's string data type, then + operator in javascript is to concat string and the closest number data type.
15th Sep 2019, 5:43 PM
Adi Pratama
Adi Pratama - avatar
+ 1
The simplest and often used way to cast a number to a string is by using the + operator. 14 + "" // "14" Whenever you are using “+” or “-” the values that you are adding or subtracting must be of the same type. Now in the above example they obviously are not. So what will happen is that JS will try to even them out — in this case cast our number to a string and then add them together. In other words every “+” expression that involves a string will result in a string. 42 + "0" // "420" We can concat strings using “+”, but what will happen if we use “-”? It can be used only on numbers, so once again, JS will cast the values to be of the same type — this time numbers. "42" - 7 // 35 "42" - 0 // 42 "42" - "9" // 33 Okay that’s not that hard, but there is always a catch. Let’s try adding two arrays together and see what happens: ["alex", "sam"] + ["jon", "mary"] // "alex,samjon,mary" Wait, what just happened? Now, whenever you use the “+” operator, the values will concatenate if one is already a string or they
30th Sep 2019, 9:56 AM
SADDAM HUSSAIN
SADDAM HUSSAIN - avatar