5 Typical JavaScript Interview Exercises

Posted on Sep 19 2014 - 10:38am by Eric Tompkins

OLYMPUS DIGITAL CAMERA

Aurelio De Rosa (sitepoint.com)

Preparing for a new developer job or interview? Here is a great article from sitepoint.com discussing some typical questions you may encounter during the interview process.

JavaScript developers are in high demand in the IT world. If this is the role that best expresses your knowledge, you have a lot of opportunities to change the company you work for and increase your salary. But before you are hired by a company, you have to demonstrate your skills in order to pass the interview process. In this article I’ll show you 5 typical questions asked for a front end job to test the JavaScript skills of the candidate and their relative solutions. It’ll be fun!

Question 1: Scope

Consider the following code:

(function() {
var a = b = 5;
})();

console.log(b);

What will be printed on the console?

Answer

The code above prints 5.

The trick of this question is that in the IIFE there are two assignments but the variable a is declared using the keyword var. What this means is that a is a local variable of the function. On the contrary, b is assigned to the global scope.

The other trick of this question is that it doesn’t use strict mode (‘use strict’;) inside the function. If strict mode was enabled, the code would raise the error Uncaught ReferenceError: b is not defined. Remember that strict mode requires you to explicitly reference to the global scope if this was the intended behavior. So, you should write:

(function() {
‘use strict’;
var a = window.b = 5;
})();

console.log(b);

Question 2: Create “native” methods

Define a repeatify function on the String object. The function accepts an integer that specifies how many times the string has to be repeated. The function returns the string repeated the number of times specified. For example:

console.log(‘hello’.repeatify(3));

Should print hellohellohello.

Answer

A possible implementation is shown below:

String.prototype.repeatify = String.prototype.repeatify || function(times) {
var str = ”;

for (var i = 0; i < times; i++) { str += this; } return str; };

The question tests the knowledge of the developer about inheritance in JavaScript and the prototype property. It also verifies that the developer is able to extend native data type functionalities (although this should not be done).

Another important point here is to demonstrate that you are aware about how to not override possible already defined functions. This is done by testing that the function didn’t exist before defining your own:

String.prototype.repeatify = String.prototype.repeatify || function(times) {/* code here */};

This technique is particularly useful when you are asked to shim a JavaScript function.

Question 3: Hoisting

What’s the result of executing this code and why.

function test() {
console.log(a);
console.log(foo());

var a = 1;
function foo() {
return 2;
}
}

test();

Answer

The result of this code is undefined and 2.

The reason is that both variables and functions are hoisted (moved at the top of the function) but variables don’t retain any assigned value. So, at the time the variable a is printed, it exists in the function (it’s declared) but it’s still undefined. Stated in other words, the code above is equivalent to the following:

function test() {
var a;
function foo() {
return 2;
}

console.log(a);
console.log(foo());

a = 1;
}

test();

Question 4: How this work in JavaScript

What is the result of the following code? Explain your answer.

var fullname = ‘John Doe’;
var obj = {
fullname: ‘Colin Ihrig’,
prop: {
fullname: ‘Aurelio De Rosa’,
getFullname: function() {
return this.fullname;
}
}
};

console.log(obj.prop.getFullname());

var test = obj.prop.getFullname;

console.log(test());

Answer

The code prints Aurelio De Rosa and John Doe. The reason is that the context of a function, what is referred with the this keyword, in JavaScript depends on how a function is invoked, not how it’s defined.

In the first console.log() call, getFullname() is invoked as a function of the obj.prop object. So, the context refers to the latter and the function returns the fullname property of this object. On the contrary, when getFullname() is assigned to the test variable, the context refers to the global object (window). This happens because test is implicitly set as a property of the global object. For this reason, the function returns the value of a property called fullname of window, which in this case is the one the code set in the first line of the snippet.

Question 5: call() and apply()

Fix the previous question’s issue so that the last console.log() prints Aurelio De Rosa.

Answer

The issue can be fixed by forcing the context of the function using either the call() or the apply() function. If you don’t know them and their difference, I suggest you to read the article What’s the difference between function.call and function.apply?. In the code below I’ll use call() but in this case apply() would produce the same result:

console.log(test.call(obj.prop));

Conclusion

In this article we’ve discussed five typical questions that are asked at interviews to test a JavaScript developer. The actual questions may differ from interview to interview but the concepts and the topics covered are usually pretty similar. I hope you had fun testing your knowledge. In case you didn’t know some of all of the answers, don’t worry: there is nothing that studying and experience can’t fix.

If you have been asked some other interesting questions at interviews, don’t hesitate to share them with us. It’ll help a lot of developers.

About the Author

Eric Tompkins is an Experienced Web Developer and Digital Media Professional. As well as a Professional Photographer and Technical Instructor. You can follow on Twitter @_codemics.