How to fix adding two numbers concatenates them instead of calculating the sum with JavaScript?

Spread the love

Sometimes, we want to fix adding two numbers concatenates them instead of calculating the sum with JavaScript.

In this article, we’ll look at how to fix adding two numbers concatenates them instead of calculating the sum with JavaScript.

How to fix adding two numbers concatenates them instead of calculating the sum with JavaScript?

To fix adding two numbers concatenates them instead of calculating the sum with JavaScript, we should convert all operands to numbers.

For instance, we write

const x = +y + +z;

to convert y and z to numbers with +.

And then we add the numbers together.

Conclusion

To fix adding two numbers concatenates them instead of calculating the sum with JavaScript, we should convert all operands to numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *