MrVitaminasG MrVitaminasG - 1 year ago 78
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();
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 Source

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());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download