number addition / string concatenation.
Last updated 2 years ago
JS ⟩ operator ⟩ arithmetic ⟩ add / concate (+)
string concatenation or number addition. 👉 table of operators
1 + 2 // number addition 'the' + 'fox' // string concatenation
replit:the + operator
// result conversion // ------------------------------------------------------------- 1 + 2, // 3 '1' + 2, // '12' (2 -> '2') '1' + '2', // '12' 1 + {}, // '1[object Object]' ({} -> object to primitive) true + true, // 2 (true -> 1) 2 + null, // 2 (null -> 0) 3 + undefined, // NaN (undefined -> NaN) // ❗ `+` mixed with strings and numbers is not associative // ------------------------------------------------------------- (1 + 2) + ' cats', // '3 cats' (addition -> concatenation) 1 + (2 + ' cats'), // '12 cats' (2 concatenations)
ℹ️ object -> primitive conversion is involved in + operation.
+
ℹ️ comparison operator favors numbers, + favors strings.
ℹ️ + operation uses punctuator plus (+).
JavaScript: The Definitive Guide ⟩ 4.8.1 The + Operator
Addition (+)