String Concatenation in JavaScript

String Concatenation in JavaScript
Rating: 5 - 1 Votes

String Concatenation in JavaScript

JavaScriptFor string concatenation in JavaScript, we make use of plus (+) operator or concat function.  But the interesting part is, the plus (+) operator is also used for adding 2 numbers.  It all depends upon how you assign a value to a variable.  A string is always surrounded by quotes whereas a number does not need to be.

If you try to concatenate or add, one string (number inside quotes) and one number (without quotes), then end result will be concatenation.  But if you use minus () operator, which is basically for subtraction, then JavaScript will treat both the variables as numbers.

Example of JavaScript string concatenation using plus (+) operator and concat function is given below:

Plus (+) Operator:

Concat Function: