Javascript variable addition | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 3

Javascript variable addition

When I try to add variables, this is what I do: var num1 = 7; var num2 = 4; document.write ("(num1) + (num2)"); Instead of writing 11, it literally says (num1) + (num2). What causes this, and how to fix it?

2nd Jul 2018, 7:36 AM
Ayn234
Ayn234 - avatar
4 Respostas
+ 3
/*Hello, Ayn234 ! Your mistake was that the Variables were in quotation marks, if any value would be in the quotes of javascript, it would assume that it was text Try this method ā†“*/ var num1 = 7; var num2 = 4; document.write((num1)+ (num2));
2nd Jul 2018, 7:45 AM
Alexander Sokolov
Alexander Sokolov - avatar
+ 1
whatever you write inside double quotates (" " ) gets printed as it is i.e. your case "(num1) + (num2)" it shall print all what comes between quotations. but if you want to deal with *value* of variable write its name so try document.write(num1+num2); here first num1 is fetched, then num2, then added and final result gets printed
2nd Jul 2018, 7:50 AM
Syed Saqeeb
Syed Saqeeb - avatar
0
That kindof works, except it writes 74
2nd Jul 2018, 7:48 AM
Ayn234
Ayn234 - avatar
0
ups, it works. i did it wrong again šŸ˜œ
2nd Jul 2018, 7:50 AM
Ayn234
Ayn234 - avatar