Categories
JavaScript Answers

How to fix JavaScript (+) sign concatenating instead of giving sum of variables?

Spread the love

Sometimes, we want to fix JavaScript (+) sign concatenating instead of giving sum of variables.

In this article, we’ll look at how to fix JavaScript (+) sign concatenating instead of giving sum of variables.

How to fix JavaScript (+) sign concatenating instead of giving sum of variables?

To fix JavaScript (+) sign concatenating instead of giving sum of variables, we should put parentheses around the expression that we want to add instead of concatenate.

For instance, we write

const divID = "question-" + (i + 1);

to wrap i + 1 with parentheses to return their sum instead of concatenating them.

Conclusion

To fix JavaScript (+) sign concatenating instead of giving sum of variables, we should put parentheses around the expression that we want to add instead of concatenate.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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