JavaScript uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
If you add two numbers, the result will be a number:
Example
var y = 20;
var z = x + y; // z will be 30 (a number)
If you add two strings, the result will be a string concatenation:
Example
var y = "20";
var z = x + y; // z will be 1020 (a string)
If you add a number and a string, the result will be a string concatenation:
Example
var y = "20";
var z = x + y; // z will be 1020 (a string)
If you add a string and a number, the result will be a string concatenation:
Example
var y = 20;
var z = x + y; // z will be 1020 (a string)
A common mistake is to expect this result to be 30:
Example
var y = 20;
var z = "The result is: " + x + y;
A common mistake is to expect this result to be 102030:
Example
var y = 20;
var z = "30";
var result = x + y + z;
The JavaScript interpreter works from left to right.
First 10 + 20 is added because x and y are both numbers.
Then 30 + "30" is concatenated because z is a string.
Practice Excercise Practice now