Understanding JavaScript by example is useful for absorbing the different concepts of the language quickly. In this post, I will illustrate a JavaScript Quiz to understand how the JavaScript operators work together. Assume that we have the following JavaScript code:

var object1 = {
valueOf: function () {
return 1;
},
toString: function () {
return "object1";
}
};
var object2 = {
valueOf: function () {
return 2;
},
toString: function () {
return "object2";
}
};
alert((object2 > object1 +-- object1) + true); //What is the output of the alert?

What is the output of the alert?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

The output of the alert is **2**. In order to understand why we have this result, we should know how the different JavaScript operators work together. Let’s analyze this quiz.

In the following expression:

(object2 > object1 +-- object1) + true

We have two main part, the first part is the expression between brackets and the second one is the boolean `true`

operand. Let’s see how the first part will be evaluated.

object2 > object1 +-- object1

In this expression, we have three main operators:

1. > operator.

2. + operator.

3. — prefix operator.

The execution order will be as follows:

1. — prefix operator.

2. + operator.

3. > operator.

To get the complete list of operator precedence, check https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Operator_Precedence.

The execution order means that the expression evaluation will start by:

-- object1

The — prefix operator will be applied on `object1`

which will result in calling the `valueOf`

method of `object1`

, which means that the result will be 1 – 1 = 0.

Then the evaluation continues by adding `object1`

to the previous result:

object1 + 0

The `valueOf`

method of `object1`

is called again, which means that the result will be 1 + 0 = 1.

Then the comparison will be performed as follows:

object2 > 1

In order to perform the comparison, the `valueOf`

method of `object2`

is called, which means that the result will be 2 > 1 = true.

Then finally we will have the following:

true + true

According to Ecma-262, if we have an arithmetic plus operator and none of its operand are of type `String`

, then both operands will be converted to numeric and the addition operation is performed. In JavaScript when `true`

is converted to Numeric, the result will be 1 (while `false`

is converted to 0).

The original addition rule according to Ecma-262 specification is as follows for your reference:

*“The addition operator either performs string concatenation or numeric addition.*

*
**The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression is evaluated as follows:*

1. Let lref be the result of evaluating AdditiveExpression.

2. Let lval be GetValue(lref).

3. Let rref be the result of evaluating MultiplicativeExpression.

4. Let rval be GetValue(rref).

5. Let lprim be ToPrimitive(lval).

6. Let rprim be ToPrimitive(rval).

7. If Type(lprim) is String or Type(rprim) is String, then

a. Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)

**8. Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim).**”