Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

The + operator: addition and string concatenation

JavaScript + adds numbers or joins strings. The types of the values determine the result, not whether their characters look like numbers.

Version: ECMAScript 2020+

Syntax

left + right

The binary + operator first obtains primitive values from both operands. If either primitive is a string, concatenation takes place: the other value is converted to text and the strings are joined. Otherwise, numeric conversion and addition take place.

2 + 3 produces the number 5, whereas "2" + 3 produces the string "23". Parentheses group operations; they do not change a value’s type by themselves. A value read using input.value is a string, including for an input with type="number".

Convert and validate input explicitly before calculating a sum. Number("2") + Number("3") produces 5. However, Number converts empty or whitespace-only strings to 0; nonnumeric text produces NaN. Check empty input and the permitted range separately.

The operator is left-associative: 1 + 2 + "3" produces "33", while "1" + 2 + 3 produces "123". Each operation uses the type of the previous result. This is deterministic behavior, not a compiler error.

A BigInt can be added to another BigInt, but 2n + 3 throws TypeError. Adding a Symbol value also throws. Objects can customize primitive conversion through Symbol.toPrimitive or valueOf; do not assume that + always calls toString.

This entry covers binary addition. Unary +value is a separate numeric conversion. In the examples below, the result’s type matters just as much as the displayed value.

Examples

const a = 2 + 3;
const b = "2" + 3;
console.log(a, typeof a);
console.log(b, typeof b);

Result

5 number
23 string
console.log(Number("2") + Number("3"));
console.log(Number(""));
console.log(Number.isNaN(Number("two")));

Result

5
0
true
console.log(1 + 2 + "3");
console.log("1" + 2 + 3);

Result

33
123
console.log((2n + 3n).toString());
try {
  console.log(2n + 3);
} catch (error) {
  console.log(error.name);
}

Result

5
TypeError

Sources