JavaScript Quiz #5

This quiz covers some of the JavaScript operators in order to explain they can work together inside expressions. Assume that we have the following JavaScript code:

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);

What is the output of the alert?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
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 by:

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

In order to check previous quizzes:
1. JavaScript Quiz #4
2. JavaScript Quiz #3
3. JavaScript Quiz #2
4. JavaScript Quiz #1

2 thoughts on “JavaScript Quiz #5

  1. Pingback: JavaScript Quiz #5 | Technical Advices, "An advice can always help"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by sweetCaptcha