Variable + Variable Javascript [solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Variable + Variable Javascript [solved]

If I have to variables like this Var first = prompt(“What ever number”) Var second = prompt(“What ever number”) document.write(first + second); Input first: 1 second: 1 Output: 11 But the output I want is: 2 How can I fix this?

23rd Nov 2018, 9:28 AM
Alexander R. Storr-Hansen
Alexander R. Storr-Hansen - avatar
6 Answers
+ 19
This happens because prompt() returns a string. Wrap your prompt() calls with this: Number.parseInt(prompt("..."));
23rd Nov 2018, 9:38 AM
Igor Makarsky
Igor Makarsky - avatar
+ 5
Alexander R. Storr-Hansen It's a common mistake for beginners and the takeaway is user inputs are always treated as string unless specified. Anyway, since your problem was already solved it would be great if you can mark the answer that you find useful to encourage the community to help each other out. 😉
23rd Nov 2018, 9:47 AM
Zephyr Koo
Zephyr Koo - avatar
+ 3
Its insane!!! i get the right help from the right people all the time. this is a really great app!! im learning so much. Thanks
23rd Nov 2018, 9:42 AM
Alexander R. Storr-Hansen
Alexander R. Storr-Hansen - avatar
+ 3
var first = +prompt(“What ever number”) // + for convert string to number var second = +prompt(“What ever number”) document.write(first + second);
23rd Nov 2018, 10:13 AM
Calviղ
Calviղ - avatar
+ 2
let me check :)
23rd Nov 2018, 9:39 AM
Alexander R. Storr-Hansen
Alexander R. Storr-Hansen - avatar
+ 1
sure
23rd Nov 2018, 9:54 AM
Alexander R. Storr-Hansen
Alexander R. Storr-Hansen - avatar