JavaScript Quiz #7

Assume that we have the following short JavaScript code:

<script>
    var str = new String("Hello");
	
    var result = typeof(str instanceof String);
    alert(result); //What is the output of the alert? 
	
    result = typeof typeof(str instanceof String);
    alert(result); //What is the output of the alert? 
	
    result = typeof typeof typeof(str instanceof String);
    alert(result); //What is the output of the alert? 	
</script>

What is the output of each alert?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
The result will be as follows boolean then string then string. Let’s understand why we have these results. In the first expression (which is a very straightforward):

var result = typeof(str instanceof String);

Will executed as follows:
1. str instanceof String will return true.
2. typeof (true) will return "boolean".

In the second expression:

var result = typeof typeof(str instanceof String);

Why does result will be string, this is because the expression will be executed as follows:
1. str instanceof String will return true.
2. typeof (true) will return "boolean" as you notice here typeof(true) returns a String that contains “boolean” value. It is important to know that JavaScript typeof operator always returns String.
3. Finally, it is now clear that typeof ("boolean") will return "string".

In the third expression:

var result = typeof typeof typeof(str instanceof String);

It is much similar to the second expression, result will return “string” because the third expression will be executed as follows:
1. str instanceof String will return true.
2. typeof (true) will return "boolean".
3. typeof ("boolean") will return "string".
3. Finally, typeof ("string") will return "string".

So now you can guess what is the result of ;):

alert(typeof typeof typeof typeof typeof typeof typeof(str instanceof String));

The best selling Amazon DE #13 Web Programming book

I would like to thank all the readers of “JavaScript Unit Testing” book, thanks to them the book is currently #13 best selling web programming in Amazon DE. I really hope that all of them find what they were looking for in my humble book.

JavaScript Unit Testing book is one of the best selling books in Amazon DE

JavaScript Unit Testing book is one of the best selling books in Amazon DE

JavaScript Quiz #6

Assume that we have the following short JavaScript code:

<script>
	var result = [12 + 21]['valueOf']()['length'];
	alert(result); //What is the output of the alert?

	result = [12 + 21]['valueOf']['length'];
	alert(result); //What is the output of the alert? 
</script>

What is the output of each alert?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
The result will be as follows 1 then 0. The first expression (the easy one) will be executed as follows:

var result = [12 + 21]['valueOf']()['length'];

The expression can be read as [33].valueOf().length which means that .length property will print the length of the valueOf() call to array [33] which is 1. Note that the [33].valueOf will return [33].

The second expression is the tricky part:

var result = [12 + 21]['valueOf']['length'];

The expression can be read as follows [33].valueOf.length. This is really completely different from the first expression because in this case the .length property will print the number of arguments of the valueOf method which is zero. This is because if the .length property is attached to a method then it will print the number of arguments of the method.

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

JavaScript Quiz #4

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

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/

Tamer Safwat’s review about JavaScript Unit Testing Book

Attached below the review of Tamer Safwat (Staff Software Engineer at Disney) about the “JavaScript Unit Testing” book:

JavaScript Unit Testing

JavaScript Unit Testing Book

Great book
“This is the most comprehensive and practical JavaScript unit testing reference I found and maybe the only one out there.

Not only the author is covering the most popular frameworks for JavaScript unit testing, he is also covering tools and plugins required to build an efficient testing environment.

The book has plenty of code samples which not only explain different unit testing scenarios but also explain many JavaScript and web programming best practices and concepts.

Overall this is a great book and a must have for anyone doing serious JavaScript work.”

Reference:
http://www.amazon.com/dp/1782160620/

The book on PacktPub:
http://www.packtpub.com/javascript-unit-testing/book

JavaScript Unit Testing Book Review by Mihai

Attached below the review of Mihai about the “JavaScript Unit Testing” book:

JavaScript Unit Testing

JavaScript Unit Testing Book

Great Book!
“I’ve just read the JavaScipt Unit Testing book and I found it very useful for professionals that need to implement or upgrade their strategy for Unit Testing.
The greatest gain, I believe, is the excellent explanation on how to use tools for Unit Testing like Jasmine, YUI, QUnit and JsTestDriver.
Great book!”

Reference:
http://www.amazon.co.uk/review/RWDCG4MJYFNZP/ref=cm_cr_dp_title?ie=UTF8&ASIN=1782160620&channel=detail-glance&nodeID=266239&store=books

The book on PacktPub:
http://www.packtpub.com/javascript-unit-testing/book

JavaScript Unit Testing Book Review by J. Powell

Attached below the review of J. Powell about the “JavaScript Unit Testing” book:

JavaScript Unit Testing

JavaScript Unit Testing Book

Great Book covering a critical topic for web applications
“The importance of JavaScript testing in single page web applications can not be overstated. When the majority of your codebase is moved to client side JavaScript, it’s more critical then ever to automate your client side testing environment in order to have a stable application. JavaScript Unit Testing teaches you to take your Test Driven and Behavior Driven development practices and apply them to the client side.”

Reference:
http://www.amazon.com/dp/1782160620/

The book on PacktPub:
http://www.packtpub.com/javascript-unit-testing/book

Review #3 about the JavaScript Unit Testing Book

Attached below the review of Werner Punz (Senior software developer for Irian GmbH. Vienna, an Apache MyFaces Committer Member of the JSF Expert Group and a JavaScript expert) about the “JavaScript Unit Testing” book:

JavaScript Unit Testing

JavaScript Unit Testing Book

Just read the book
“The book is very good, it covers the theory behind unit testing and then goes into details by applying various unit testing frameworks in a small and a real world Ajax example.
The testframeworks covered are

– Jasmine
– YUI Test
– QUnit
-JSTestdriver

This book is a must read for everyone interested into Javascript unit testing, especially if you have to cover asynchronousity via Ajax (which is a key focal point through the entire book, in the more complex example).

Overall a very good book. Five stars from me.”

Reference:
http://www.amazon.com/dp/1782160620/

The book on PacktPub:
http://www.packtpub.com/javascript-unit-testing/book