JavaScript add the number to a string | Explanation & Example code

How to add a number and a string in JavaScript?

In JavaScript, the plus + the operator is used for both numeric addition and string concatenation. To adds the number to a string Just use plus between the number and string in Javascript.

String + Number = Concatenated string
Number + Number = The sum of both numbers
StringNumber = The difference between (coerced string) and the number

Note: When you “add” a number to a string the interpreter converts your number to a string and concatenates both together.

Example of JavaScript add the number to a string

If we try to add a number and a string then, as addition is not possible, ‘concatenation’ takes place.

In the example adding a number (integer) and string, also checking the output type.

Output:

JavaScript add the number to a string

Some Examples:-

Do comment if you have any questions and suggestion on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.