# JavaScript Quiz #5

### In this installment of the JavaScript Quiz, we're building on the knowledge gained from the previous quizzes and learning about the comma operator. Good luck!

Join the DZone community and get the full member experience.

Join For FreeIf you haven't checked out JavaScript quizzes 1, 2, 3, and 4, take some time to look at those as well. Each one builds on the other and they get progressively more difficult. See if you really know your JavaScript order of operations.

```
var object1 = {
valueOf: function () {
return 10;
},
toString: function () {
return "object1";
}
};
var object2 = {
valueOf: function () {
return 20;
},
toString: function () {
return "object2";
}
};
var object3 = {
valueOf: function () {
return 30;
},
toString: function () {
return "object3";
}
};
var result = (object2, object1, object3) + object1 +-- object1;
alert(result);
```

__Question__: What is the output of the alert and why?

**Write your answer on a piece of paper and then read the answer.**

__Answer__:

The output of the alert is **49**. Let’s analyze this quiz. In the following expression:

`(object2, object1, object3)`

In order to know the result of this expression, we have to know how expressions work with the comma operator. If we have an expression that contains many comma operators then this expression will be evaluated to the last mentioned value. This means that `(object2, object1, object3)`

will be evaluated to `object3`

. We will then have the following reduced expression:

`object3 + object1 +-- object1`

In this expression, we have two main operators:

1. + operator.

2. -- prefix operator.

The execution order will be as follows:

1. -- prefix operator.

2. + 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 with:

`-- object1`

The -- prefix operator will be applied on `object1`

which will result in calling the `valueOf`

method of `object1`

. This means that the result will be 10 – 1 = 9.

Then the evaluation execution continues by adding `object3 + object1`

to the previous result as follows:

`object3 + object1 + 9`

Now, we come to the simplest part of the quiz, the `valueOf`

methods of `object1`

and `object3`

are called, which means that the final result will be 30 + 10 + 9 = 49.

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).**”

Published at DZone with permission of Hazem Saleh, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Comments