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 (+)