JavaScript + operator

Notes:

JavaScript + operator:

Expression:
A valid combination of operators and operands is known as an expression.
An expression contains two elements, they are operators and operands.
Operator: is going to perform an operation on the given operands.

Ex:
2 + 2
2 + 2 + 2
2 + 2 + 2 + 2

Understanding the JavaScript + operator:
JavaScript + operator accepts 2 operands:

JavaScript + operator is an overloaded operator, that means it acts differently in different situations.
If one of the given operand is of string type, then the + operator acts as a concatenation operator.
If both operands are of number type, then the + operator acts as an addition operator.
Operand1 + Operator Operand2 Result
string + (concatenation) string string
string + (concatenation) number string
number + (concatenation) string string
number + (addition) number number

Note: if there are more than one + operators in an expression, computer evaluates the expression from left to right.

Ex:
“2” + “2” = “22”
“2” + 2 = “22”
2 + “2” = “22”
2 + 2 = 4

Interview Questions: