I am adding two numbers with jQuery and I need to get the result in NEGATIVE even if the integers are positive, i.e
var sum = (5 + 10);
console.log(sum) // Expected result is -15
// Current result is 15
Tried concatenating like
sum = '-' + (a + b) // But since the string is being added to number it's not working the expected way.
Need a little help on the same. Thanks
Don't use
-as string, when you use'-'with+operator, the number is casted to string and the result will be'-15'as string. In this case+operator is used as string concatenation operator.Using
'-'in the expression, you're not using this as operator, it is string. Use-without quotes to use it as arithmetic minus operator.This will give you the negated result. If the result of the expression is itself negative e.g.
(-10 + 5)above expression will give you result5.In case if you always want the negative result