How to add 2 variab...
 
Notifications
Clear all

How to add 2 variables in javascript ?

2 Posts
2 Users
0 Reactions
775 Views
(@manu47)
Active Member
Joined: 5 years ago
Posts: 3
Topic starter  

I have two variables that I would like to add in javascript but unfortunately I cannot find the syntax.

Example:
toto = '1';
tata = '2';
result = toto + tata;

return result;

Unfortunately the result obtained is 12 whereas I would like it to be 3.

Do you have an idea ?

Thanks in advance.


   
Quote
Smiddyy
(@smiddyy)
Active Member
Joined: 1 year ago
Posts: 7
 

You placed the 1 and 2 between '' in the variables.

Then it's a sort of name instead of a number, and cant be calculated. It simply places the names after eachother. So you get 12. When you name them x and y you get xy

Without the '' it becomes a number

This should work:

 

var toto = 1;
var tata = 2;
var result = '';

result = toto+tata;

return result;

 

or:

 

var toto = 1;
var tata = 2;

return toto+tata

 

 

This post was modified 9 months ago 2 times by Smiddyy

   
ReplyQuote
Share: