DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. JavaScript Quiz #1 and #2

JavaScript Quiz #1 and #2

Hazem Saleh user avatar by
Hazem Saleh
·
Jul. 08, 15 · Tutorial
Like (4)
Save
Tweet
Share
4.66K Views

Join the DZone community and get the full member experience.

Join For Free

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!

JavaScript

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Future of Cloud Engineering Evolves
  • The Role of Data Governance in Data Strategy: Part II
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid
  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: