37 Essential JavaScript Interview Questions and Answers (2022) Toptal®
37 Essential JavaScript Interview Questions and Answers (2022) Toptal®
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.
INTERVIEW QUESTIONS
View answer
2. What will the code below output to the console and why?
(function(){
var a = b = 3;
})();
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 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
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
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
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
(a) What gets logged to the console when the user clicks on “Button 4”
and why?
var d = {};
View answer
13. What will the code below output to the console and why?
View answer
14. What will the code below output to the console and why ?
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?
if (item) {
// process the list item...
nextListItem();
}
};
View answer
View answer
17. What would the following lines of code output to the console?
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
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;
}
};
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.
• a DOM element
View answer
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
var x = 21;
var girl = function () {
console.log(x);
var x = 20;
};
girl ();
View answer
View answer
View answer
View answer
a[10] = 99;
console.log(a[6]);
View answer
View answer
By continuing to use this site you agree to our Cookie Policy. Got it
What would following code return?
32.
View answer
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
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
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
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.
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 *
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
Submit a Question
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
CSS Full-stack
Tyler Standley
Freelance JavaScript Developer
United States Toptal Member Since June 25, 2018
Justin Michela
Freelance JavaScript Developer
United States Toptal Member Since March 28, 2018
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.
Learn more
React.js Top 3%
JavaScript Clients
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
®
By continuing to use this site you agree to our Cookie Policy. Got it