quote:
var number1='5';
var number2='4';
var result=number1+number2;
...
but the result is 54, and not 9. why ?
To answer your question for your specific example: because you have indicated the values are strings by using single quotes. Remove the quotes and try the following:
code:
var number1 = 5;
var number2 = 4;
var result = number1 + number2;
Now you should be getting 9 as the result.
Of course, you won't always be able to do this when your values are coming from somewhere else and happen to be strings. When that happens you will want to check out
parseInt() and
parseFloat():
code:
var number1 = '5';
var number2 = '4';
var result = parseInt(number1) + parseInt(number2);
This should yield 9 as well.