+ 4

Help! '2'+'2'-'2'=20 how??

if '2'+'2'=22 then 22-'2' should be 2 why it outputs 20

8th Feb 2018, 5:22 PM
Sudarshan Rai
Sudarshan Rai - avatar
9 Answers
+ 9
var a = 15; var b = "3"; alert(a+b);// 153 alert(a-b);//12 alert(a/b);//5 alert(a*b);//45 When we use adding it will sum as strings. Otherwise it will operate as numbers. Such rules are at javascript. It is needed to learn by heart.
8th Feb 2018, 7:14 PM
Vitaliy Angelov (Rostov-on-Don)
Vitaliy Angelov (Rostov-on-Don) - avatar
+ 16
in JS we use + sign for addition & concatenation. in the case of numbers + is addition. in the case of strings + is concatenation. anything inside quotation marks is a string right? so '2'+'2' means concatenate the two strings. however any other operation will treat the number in quotation as a number not a string hence the second line treats the '2' as a number, so 22-'2'=20.
8th Feb 2018, 5:59 PM
Mazin Ayash
Mazin Ayash - avatar
8th Feb 2018, 6:21 PM
Mazin Ayash
Mazin Ayash - avatar
+ 7
You are wellcome!🙌â˜ș🙏
8th Feb 2018, 7:33 PM
Vitaliy Angelov (Rostov-on-Don)
Vitaliy Angelov (Rostov-on-Don) - avatar
+ 5
You're putting single quotes around the number, which makes it a string instead of a number. Since math uses numbers, you not calculating anything but simply concatenating the two strings together, which is why you get 22. In the next line, you didn't place quotes around 22, so it's treated as a number and it's changing the string to an int for you; the first number of the calculation is automatically converting '2' to 2.
8th Feb 2018, 5:30 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
thnx @jakob for answer but why the last '2' changes to 2 (int) automaticlly ,when we have already decleared that as '2'(string)
8th Feb 2018, 5:39 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 2
@Shudarshan Sorry, I'm at work and didn't realize you responded. It's called implicit type casting. Basically, the first variable in the equation is type casting onto the second variable (since they're different types) to convert it for the calculation. You can explicitly do the type casting yourself, but this is sort of a "shortcut" for it to make things easier, as well as preventing accidental errors due to not properly converting variables when necessary or trying to calculate between incorrect data types. *edit* Just remembered we're dealing with JS, but the principal is the same in regard to converting types. JS likes to automatically do a lot of things more than most other languages. However, the implicit type casting works the same in many of the other languages.
8th Feb 2018, 6:35 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
@Shudarshan https://www.w3schools.com/js/js_type_conversion.asp ^Go there and scroll down to the section "Automatic Type Conversion" and read from there. It'll explain it perfectly for you. While you're at it, won't hurt to read the whole page. lol
8th Feb 2018, 6:39 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
thnx all guys😁😁
8th Feb 2018, 7:24 PM
Sudarshan Rai
Sudarshan Rai - avatar