Can anyone say whether the following code is possible or not ? If possible then please give expected output and explanation. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone say whether the following code is possible or not ? If possible then please give expected output and explanation.

a=prompt(); document.write((a+=10)+"<br/>"+(a-=11));

17th Jan 2020, 2:24 PM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
4 Answers
+ 6
Why use a+=10 if you can simply use a+10 ? += only changes the value of the variable, but does not return anything.
17th Jan 2020, 2:26 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Why don't you try it yourself 🙂🙃👇 https://code.sololearn.com/W1hcwlLp15w7/?ref=app
17th Jan 2020, 2:27 PM
Arsenic
Arsenic - avatar
+ 2
I have tried it and I have got the output. But I am seeking for the explanation of the output…
17th Jan 2020, 2:33 PM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
0
Prompt returns a string. When JavaScript tries to operate on a "wrong" data type, it will try to convert the value to a "right/correct" type. 5 + null // returns 5 because null is converted to 0 "5" + null // returns "5null" because null is converted to "null" "5" + 2 // returns "52" because 2 is converted to "2" "5" - 2 // returns 3 because "5" is converted to 5 "5" * "2" // returns 10 because "5" and "2" are converted to 5 and 2 reference from W3Schools.com https://code.sololearn.com/WcDytXNgw5XL/#js
17th Jan 2020, 4:46 PM
ODLNT
ODLNT - avatar