JavaScript Concatenation Operation: The Plus Sign

The JavaScript concatenation operator is the plus sign. As shown in the example below, the plus sign in JavaScript can be used to concatenate strings:

var strA = 'Joey';
var strB = 'JavaScript';
var strC = strA + ' ' + strB;
document.write(strC);//Result is Joey JavaScript

Strings can also be concatenated with the shorthand assignment operator +=, as follows:

var strA = 'Joey';
var strB = ' JavaScript';
var strC = strA += strB;
document.write(strC);//Result is Joey JavaScript

A number concatenated with a string, results in a string, as follows:

var strA = 'Joey';
var strB = 5;
var strC = strA += strB;
document.write(strC); //Result is Joey5

If two numbers are concatenated together, JavaScript is no longer concatenating, as this is now an addition operation:

var strA = 5;
var strB = 5;
var strC = strA += strB;
document.write(strC);//Result is 10

Had one or both of the numbers been in quotes, however, JavaScript will treat this as a string operation:

var strA = '5';
var strB = 5;
var strC = strA += strB;
document.write(strC);//Result is 55

Leave a Reply