Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JavaScript Quiz #1 and #2

DZone's Guide to

JavaScript Quiz #1 and #2

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Taking coding quizzes from time to time helps refresh our programming knowledge. This is why I decided to post some JavaScript quizzes. They will definitely test your knowledge of some important JavaScript concepts.

Assume that we have the following JavaScript code:

var someVar = 1;
var someObject = {
someVar: 2,
someMethod: function () {
var someVar = 3;

alert(this.someVar); //What is the result ???

setTimeout(function(){
alert(this.someVar); //What is the result ???
}, 10);
}
};

someObject.someMethod();


Question: What is the output in each alert and why?

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


Answer:

In order to understand how this JavaScript example will work, we should know how the JavaScript “this” keyword works. It is important to know that within the body of a JavaScript method, “this” evaluates to the object on which the method was invoked.

This means that in the “this.someVar” expression of the first alert, “this” will be evaluated to the someObject object on which the someMethod method was invoked, which will result in 2 (the value set in line 3).

In the “this.someVar” expression of the second alert, “this” will be evaluated to the Window global object because of the window.setTimeout, which will result in 1 (the value set in line 1).

Did you get the answer right?  If so, pat yourself on the back.  And get ready for the next quiz!


JavaScript Quiz #2

Assume that we have the following JavaScript code:

var someVar = "1";
var someObject = {
someVar: 2,
someMethod: function () {
var someVar = 3;
var _this = this;

setTimeout(function() {
var result = _this.someVar + this.someVar;

alert(result); //What is the value of result here?

if (result > 20) {
+result--;
} else {
+result++;
}

alert(result); //What is the value of result here?

}, 10);
}
};

someObject.someMethod();

Question: What is the output in each alert and why?

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


Answer:

In order to understand how this JavaScript example will work, we should know the following:
1. How the JavaScript “this” keyword works.
2. How the JavaScript operators work.

I illustrated in detail how the JavaScript “this” keyword works above: “this” evaluates to the object on which the method was invoked. This means that _this.someVar inside the setIimeout will be evaluated to 2.

It is important to understand how the "+" arithmetic operator works, in the following line:

var result = _this.someVar + this.someVar;

The value of result in this line will be evaluated to: 2 + “1” = “21”. This is because the "+" arithmetic JavaScript operator always favors strings, which means that if you have a numeric + String, the numeric operand will be converted to a string and the result of the "+" arithmetic operator will be a simple string concatenation.

Coming to the next line:

result > 20

Here there is a comparison between both a string and a number, unlike the "+" arithmetic operator, the JavaScript relational operators favor numbers, which means that result will be converted to an integer so the comparison will be 21 > 20 which will be evaluated to true.

Finally in the following line:

+result--;

The "+" unary operator can be used to convert its operand to an integer, however, it has no effect here as the evaluation of the expression. The post-increment operator is executed before it, which will convert result to an integer and decrement it by 1.

As a result, the alerts will produce the following output in this order:
1. 21
2. 20

Keep an eye out for the next quiz!

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:
javascript ,web dev

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}