You are on page 1of 17

37 Essential JavaScript Interview

Questions *
Toptal sourced essential questions that the best JavaScript developers and
engineers can answer. Driven from our community, we encourage experts to
submit questions and offer feedback.

Apply as a JavaScript Developer

INTERVIEW QUESTIONS

1. What is a potential pitfall with using typeof bar === "object" to


determine if bar is an object? How can this pitfall be avoided?

View answer

2. What will the code below output to the console and why?

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

console.log("a defined? " + (typeof a !== 'undefined'));


console.log("b defined? " + (typeof b !== 'undefined'));

By continuing to use this site you agree to our Cookie Policy. Got it
View answer

3. What will the code below output to the console and why?

var myObject = {
foo: "bar",
func: function() {
var self = this;
console.log("outer func: this.foo = " + this.foo);
console.log("outer func: self.foo = " + self.foo);
(function() {
console.log("inner func: this.foo = " + this.foo);
console.log("inner func: self.foo = " + self.foo);
}());
}
};
myObject.func();

View answer

Apply to Join Toptal's Development Network


and enjoy reliable, steady, remote Freelance JavaScript Developer Jobs

Apply as a Freelancer

4. What is the significance of, and reason for, wrapping the entire
content of a JavaScript source file in a function block?

View answer

By continuing to use this site you agree to our Cookie Policy. Got it
What is the significance, and what are the benefits, of including
5. 'use

strict' at the beginning of a JavaScript source file?

View answer

6. Consider the two functions below. Will they both return the same
thing? Why or why not?

function foo1()
{
return {
bar: "hello"
};
}

function foo2()
{
return
{
bar: "hello"
};
}

View answer

7. What will the code below output? Explain your answer.

console.log(0.1 + 0.2);
console.log(0.1 + 0.2 == 0.3);

View answer

8. In what order will the numbers 1-4 be logged to the console when the
code below is executed? Why?
By continuing to use this site you agree to our Cookie Policy. Got it
(function() {
console.log(1);
setTimeout(function(){console.log(2)}, 1000);
setTimeout(function(){console.log(3)}, 0);
console.log(4);
})();

View answer

9. Write a simple function (less than 160 characters) that returns a


boolean indicating whether or not a string is a palindrome.

View answer

10. Write a sum method which will work properly when invoked using
either syntax below.

console.log(sum(2,3)); // Outputs 5
console.log(sum(2)(3)); // Outputs 5

View answer

11. Consider the following code snippet:

for (var i = 0; i < 5; i++) {


var btn = document.createElement('button');
btn.appendChild(document.createTextNode('Button ' + i));
btn.addEventListener('click', function(){ console.log(i); });
document.body.appendChild(btn);
}

(a) What gets logged to the console when the user clicks on “Button 4”
and why?

(b) Provide one or more alternate implementations that will work as


expected.
By continuing to use this site you agree to our Cookie Policy. Got it
View answer

12. Assuming d is an “empty” object in scope, say:

var d = {};

…what is accomplished using the following code?

[ 'zebra', 'horse' ].forEach(function(k) {


d[k] = undefined;
});

View answer

13. What will the code below output to the console and why?

var arr1 = "john".split('');


var arr2 = arr1.reverse();
var arr3 = "jones".split('');
arr2.push(arr3);
console.log("array 1: length=" + arr1.length + " last=" + arr1.slice(-1));
console.log("array 2: length=" + arr2.length + " last=" + arr2.slice(-1));

View answer

14. What will the code below output to the console and why ?

console.log(1 + "2" + "2");


console.log(1 + +"2" + "2");
console.log(1 + -"1" + "2");
console.log(+"1" + "1" + "2");
console.log( "A" - "B" + "2");
console.log( "A" - "B" + 2);

View answer
By continuing to use this site you agree to our Cookie Policy. Got it
15. following recursive code will cause a stack overflow if the array list
The
is too large. How can you fix this and still retain the recursive pattern?

var list = readHugeList();

var nextListItem = function() {


var item = list.pop();

if (item) {
// process the list item...
nextListItem();
}
};

View answer

16. What is a “closure” in JavaScript? Provide an example.

View answer

17. What would the following lines of code output to the console?

console.log("0 || 1 = "+(0 || 1));


console.log("1 || 2 = "+(1 || 2));
console.log("0 && 1 = "+(0 && 1));
console.log("1 && 2 = "+(1 && 2));

Explain your answer.

View answer

18. What will be the output when the following code is executed?
Explain.

console.log(false == '0')
By continuing to use this site
console.log(false ===you agree to our Cookie Policy.
'0') Got it
View answer

19. What is the output out of the following code? Explain your answer.

var a={},
b={key:'b'},
c={key:'c'};

a[b]=123;
a[c]=456;

console.log(a[b]);

View answer

20. What will the following code output to the console:

console.log((function f(n){return ((n > 1) ? n * f(n-1) : n)})(10));

Explain your answer.

View answer

21. Consider the code snippet below. What will the console output be
and why?

(function(x) {
return (function(y) {
console.log(x);
})(2)
})(1);

View answer

By continuing to use this site you agree to our Cookie Policy. Got it
22. What will the following code output to the console and why:
var hero = {
_name: 'John Doe',
getSecretIdentity: function (){
return this._name;
}
};

var stoleSecretIdentity = hero.getSecretIdentity;

console.log(stoleSecretIdentity());
console.log(hero.getSecretIdentity());

What is the issue with this code and how can it be fixed.

View answer

23. Create a function that, given a DOM Element on the page, will visit
the element itself and all of its descendents (not just its immediate
children). For each element visited, the function should pass that
element to a provided callback function.

The arguments to the function should be:

• a DOM element

• a callback function (that takes a DOM element as its argument)

View answer

24. Testing your this knowledge in JavaScript: What is the output of


the following code?

var length = 10;


function fn() {
console.log(this.length);
}

var obj = {
By continuing to use
length: 5, this site you agree to our Cookie Policy. Got it
method: function(fn) {
fn();
arguments[0]();
}
};

obj.method(fn, 1);

View answer

25. Consider the following code. What will the output be, and why?

(function () {
try {
throw new Error();
} catch (x) {
var x = 1, y = 2;
console.log(x);
}
console.log(x);
console.log(y);
})();

View answer

26. What will be the output of this code?

var x = 21;
var girl = function () {
console.log(x);
var x = 20;
};
girl ();

View answer

27. for (let i = 0; i < 5; i++) {


By continuingsetTimeout(function()
to use this site you agree{ to our Cookie Policy.
console.log(i); }, i * 1000 ); Got it
}
What will this code print?

View answer

28. What do the following lines output, and why?

console.log(1 < 2 < 3);


console.log(3 > 2 > 1);

View answer

29. How do you add an element at the begining of an array? How do


you add one at the end?

View answer

30. Imagine you have this code:

var a = [1, 2, 3];

a) Will this result in a crash?

a[10] = 99;

b) What will this output?

console.log(a[6]);

View answer

31. What is the value of typeof undefined == typeof NULL ?

View answer
By continuing to use this site you agree to our Cookie Policy. Got it
What would following code return?
32.

console.log(typeof typeof 1);

View answer

33. What will be the output of the following code:

for (var i = 0; i < 5; i++) {


setTimeout(function() { console.log(i); }, i * 1000 );
}

Explain your answer. How could the use of closures help here?

View answer

34. What is NaN ? What is its type? How can you reliably test if a value is
equal to NaN ?

View answer

35. What will the following code output and why?

var b = 1;
function outer(){
var b = 2
function inner(){
b++;
var b = 3;
console.log(b)
}
inner();
}
outer();

By continuing to use this site you agree to our Cookie Policy. Got it
View answer
36. Discuss possible ways to write a function isInteger(x) that
determines if x is an integer.

View answer

37. How do you clone an object?

View answer

* There is more to interviewing than tricky technical questions, so these are intended merely as a guide. Not
every “A” candidate worth hiring will be able to answer them all, nor does answering them all guarantee an “A”
candidate. At the end of the day, hiring remains an art, a science — and a lot of work.

WHY TOPTAL

Tired of interviewing candidates? Not sure what to ask to get you a


top hire?
Let Toptal find the best people for you.

Hire a Top JavaScript Developer Now

OUR EXCLUSIVE NETWORK OF JAVASCRIPT DEVELOPERS

Jay Johnston
Freelance JavaScript Developer

By continuing to use this site you agree to our Cookie Policy. Got it
Looking to land a job as a JavaScript Developer?
Let Toptal find the right job for you.

Apply as a JavaScript Developer

JOB OPPORTUNITIES FROM OUR NETWORK

Remote Full-Stack JavaScript Developer Job for a Sales Automation Platform (Part-Time)
1 day ago・Remote・Part Time

Submit an interview question
Submitted questions and answers are subject to review and editing, and may or may not be
selected for posting, at the sole discretion of Toptal, LLC.

Name *

Email *

Enter Your Question Here … *

By continuing to use this site you agree to our Cookie Policy. Got it
Enter Your Answer Here … *

* I agree with the Terms and Conditions of Toptal, LLC's Privacy Policy

* All fields are required

Submit a Question

Looking for JavaScript Developers?


Looking for JavaScript Developers? Check out Toptal’s JavaScript developers.

By continuing to use this site you agree to our Cookie Policy. Got it
Jay Johnston
Freelance JavaScript Developer
United States Toptal Member Since November 6, 2013

Coding HTML, CSS, and JS since his armed forces days in 1997, Jay's
experience in adapting web technology to meet customer needs is
extensive. He enjoys bringing value to clients via eCommerce solution…
Show More

View Jay JavaScript PHP Node.js jQuery LAMP SQL

CSS Full-stack

Tyler Standley
Freelance JavaScript Developer
United States Toptal Member Since June 25, 2018

Along with strong communication skills and an exemplary work ethic,


Tyler brings his hands-on experience with a wide range of programming
languages. Recently, though, his focus has been directed towards…
Show More

View Tyler JavaScript Amazon Web Services (AWS) AWS

Justin Michela
Freelance JavaScript Developer
United States Toptal Member Since March 28, 2018

Justin is a technical professional with a passion for learning and 15+


years of experience leading teams to build enterprise-grade distributed
applications that solve real-world problems. He is a firm believer that…
Show More

View Justin JavaScript Java React Native Freelance C#

Spring TypeScript Back-end Development Python

By continuing to use this site you agree to our Cookie Policy. Got it
Toptal Connects the Top 3% of Freelance
Talent All Over The World.

Join the Toptal community.

Learn more

Skills in High Demand by Clients About

React.js Top 3%

JavaScript Clients

Python Toptal Talent Assessment

WordPress Ultimate Freelancing Guide

Node.js Blog

Java Community

About Us

Contact Social

Contact Us

Press Center

Careers
By continuing to use this site you agree to our Cookie Policy. Got it
FAQ
®

The World’s Top Talent, On Demand ™

Copyright 2010 - 2022 Toptal, LLC Privacy Policy Website Terms

By continuing to use this site you agree to our Cookie Policy. Got it

You might also like