+ 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?
4 Antworten
+ 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));
+ 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
0
That kindof works, except it writes 74
0
ups, it works. i did it wrong again 😜