MrVitaminasG MrVitaminasG - 4 months ago 6
Javascript Question

How to sum two different functions in javascript

I didn't thought that is a problem. I have different function which returns values; So i created different function to sum both values.

function result(){
var result = firstFunct() + secondFunct();
alert(result);
return result;
}


And it gives for me a result like joint of two strings: 1010 instead 20. What kind of operator i need to use two sum both of them.

Answer

This is most probably because your functions are returning string and not a numeric data type. You will need to cast the output of the functions

var result = Number(firstFunct()) + Number(secondFunct());

note, casting to a number could also be done in a shorter way, usin the + operater:

var result = (+firstFunct()) + (+secondFunct());