JavaScript Quiz #3

Understanding JavaScript by example is useful for absorbing the different concepts of the language quickly. In this post, I will illustrate a quick JavaScript Quiz to understand how JavaScript (+) operator works. Assume that we have the following JavaScript code:

var object1 = {
    someVar: 2,
    someName: "object1",
    valueOf: function () {	
        return this.someVar;
    },
    toString: function () {	
        return this.someName;
    }
};

var result = "I equal " + object1;

alert(result); //What is the output of the alert?

What is the output of the alert?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
The output of the alert is “I equal 2″. In order to understand why we have this result, we should know how JavaScript (+) operator works. Normally, as you may know in JavaScript, the (+) operator favors String, which means that if we have a String + Number then Number will be converted to a String and the result will be a concatenation between the two String objects.

In case that we have a String + Object, then toString() method will be called on the Object‘s valueOf() method, this is why in the line:

"I equal " + object1;

The right hand side value of the expression will be evaluated to “2″ which will be concatenated with the left hand side value to have finally the result “I equal 2″.

The original 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 get Quiz #1 and #2, check:
1. JavaScript Quiz #1:
http://www.technicaladvices.com/2013/03/14/javascript-quiz-1/
2. JavaScript Quiz #2
http://www.technicaladvices.com/2013/03/28/javascript-quiz-2/

4 thoughts on “JavaScript Quiz #3

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

  2. I’ve read several good stuff here. Certainly worth bookmarking for revisiting. I wonder how much attempt you put to create the sort of excellent informative web site.

  3. Hi there, You’ve done an excellent job. I’ll definitely digg it and in my view suggest to my friends. I am sure they will be benefited from this site.

  4. really impressed! everything is very open and very clear explanation of issues. it contains truly information. your website is very useful. thanks for sharing. looking forward to more!

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 sweet Captcha