JavaScript trivia questions in front end interviews
Front End Interview Handbook is now part of GreatFrontEnd! We are working to migrate the content over and you may find the latest version of this page on GreatFrontEnd.
Answers to Front-end Job Interview Questions - JS Questions. Pull requests for suggestions and corrections are welcome!
- Explain event delegation
- Explain how
this
works in JavaScript - Explain how prototypal inheritance works
- What do you think of AMD vs CommonJS?
- Explain why the following doesn't work as an IIFE:
function foo(){ }();
. What needs to be changed to properly make it an IIFE? - What's the difference between a variable that is:
null
,undefined
or undeclared? How would you go about checking for any of these states? - What is a closure, and how/why would you use one?
- Can you describe the main difference between a
.forEach
loop and a.map()
loop and why you would pick one versus the other? - What's a typical use case for anonymous functions?
- How do you organize your code? (module pattern, classical inheritance?)
- What's the difference between host objects and native objects?
- Difference between:
function Person(){}
,var person = Person()
, andvar person = new Person()
? - What's the difference between
.call
and.apply
? - Explain
Function.prototype.bind
. - When would you use
document.write()
? - What's the difference between feature detection, feature inference, and using the UA string?
- Explain Ajax in as much detail as possible.
- What are the advantages and disadvantages of using Ajax?
- Explain how JSONP works (and how it's not really Ajax).
- Have you ever used JavaScript templating? If so, what libraries have you used?
- Explain "hoisting".
- Describe event bubbling.
- What's the difference between an "attribute" and a "property"?
- Why is extending built-in JavaScript objects not a good idea?
- Difference between document
load
event and documentDOMContentLoaded
event? - What is the difference between
==
and===
? - Explain the same-origin policy with regards to JavaScript.
- Make this work:
- Why is it called a Ternary expression, what does the word "Ternary" indicate?
- What is
"use strict";
? What are the advantages and disadvantages to using it? - Create a for loop that iterates up to
100
while outputting "fizz" at multiples of3
, "buzz" at multiples of5
and "fizzbuzz" at multiples of3
and5
. - Why is it, in general, a good idea to leave the global scope of a website as-is and never touch it?
- Why would you use something like the
load
event? Does this event have disadvantages? Do you know any alternatives, and why would you use those? - Explain what a single page app is and how to make one SEO-friendly.
- What is the extent of your experience with Promises and/or their polyfills?
- What are the pros and cons of using Promises instead of callbacks?
- What are some of the advantages/disadvantages of writing JavaScript code in a language that compiles to JavaScript?
- What tools and techniques do you use for debugging JavaScript code?
- What language constructions do you use for iterating over object properties and array items?
- Explain the difference between mutable and immutable objects.
- Explain the difference between synchronous and asynchronous functions.
- What is event loop? What is the difference between call stack and task queue?
- Explain the differences on the usage of
foo
betweenfunction foo() {}
andvar foo = function() {}
- What are the differences between variables created using
let
,var
orconst
? - What are the differences between ES6 class and ES5 function constructors?
- Can you offer a use case for the new arrow => function syntax? How does this new syntax differ from other functions?
- What advantage is there for using the arrow syntax for a method in a constructor?
- What is the definition of a higher-order function?
- Can you give an example for destructuring an object or an array?
- ES6 Template Literals offer a lot of flexibility in generating strings, can you give an example?
- Can you give an example of a curry function and why this syntax offers an advantage?
- What are the benefits of using spread syntax and how is it different from rest syntax?
- How can you share code between files?
- Why might you want to create static class members?
- Other Answers
Explain event delegation
Event delegation is a technique involving adding event listeners to a parent element instead of adding them to the descendant elements. The listener will fire whenever the event is triggered on the descendant elements due to event bubbling up the DOM. The benefits of this technique are:
- Memory footprint goes down because only one single handler is needed on the parent element, rather than having to attach event handlers on each descendant.
- There is no need to unbind the handler from elements that are removed and to bind the event for new elements.
References
- https://davidwalsh.name/event-delegate
- https://stackoverflow.com/questions/1687296/what-is-dom-event-delegation
Explain how this
works in JavaScript
There's no simple explanation for this
; it is one of the most confusing concepts in JavaScript. A hand-wavey explanation is that the value of this
depends on how the function is called. I have read many explanations on this
online, and I found Arnav Aggrawal's explanation to be the clearest. The following rules are applied:
- If the
new
keyword is used when calling the function,this
inside the function is a brand new object. - If
apply
,call
, orbind
are used to call/create a function,this
inside the function is the object that is passed in as the argument. - If a function is called as a method, such as
obj.method()
—this
is the object that the function is a property of. - If a function is invoked as a free function invocation, meaning it was invoked without any of the conditions present above,
this
is the global object. In a browser, it is thewindow
object. If in strict mode ('use strict'
),this
will beundefined
instead of the global object. - If multiple of the above rules apply, the rule that is higher wins and will set the
this
value. - If the function is an ES2015 arrow function, it ignores all the rules above and receives the
this
value of its surrounding scope at the time it is created.
For an in-depth explanation, do check out his article on Medium.
Can you give an example of one of the ways that working with this has changed in ES6?
ES6 allows you to use arrow functions which uses the enclosing lexical scope. This is usually convenient, but does prevent the caller from controlling context via .call
or .apply
—the consequences being that a library such as jQuery
will not properly bind this
in your event handler functions. Thus, it's important to keep this in mind when refactoring large legacy applications.
References
- https://codeburst.io/the-simple-rules-to-this-in-javascript-35d97f31bde3
- https://stackoverflow.com/a/3127440/1751946
Explain how prototypal inheritance works
All JavaScript objects have a __proto__
property with the exception of objects created with Object.create(null)
, that is a reference to another object, which is called the object's "prototype". When a property is accessed on an object and if the property is not found on that object, the JavaScript engine looks at the object's __proto__
, and the __proto__
's __proto__
and so on, until it finds the property defined on one of the __proto__
s or until it reaches the end of the prototype chain. This behavior simulates classical inheritance, but it is really more of delegation than inheritance.
Example of Prototypal Inheritance
// Parent object constructor.
function Animal(name) {
this.name = name;
}
// Add a method to the parent object's prototype.
Animal.prototype.makeSound = function () {
console.log('The ' + this.constructor.name + ' makes a sound.');
};
// Child object constructor.
function Dog(name) {
Animal.call(this, name); // Call the parent constructor.
}
// Set the child object's prototype to be the parent's prototype.
Object.setPrototypeOf(Dog.prototype, Animal.prototype);
// Add a method to the child object's prototype.
Dog.prototype.bark = function () {
console.log('Woof!');
};
// Create a new instance of Dog.
const bolt = new Dog('Bolt');
// Call methods on the child object.
console.log(bolt.name); // "Bolt"
bolt.makeSound(); // "The Dog makes a sound."
bolt.bark(); // "Woof!"
Things to note are:
.makeSound
is not defined onDog
, so the engine goes up the prototype chain and finds.makeSound
off the inheritedAnimal
.- Using
Object.create
to build the inheritance chain is no longer recommended. UseObject.setPrototypeOf
instead.
References
- http://dmitrysoshnikov.com/ecmascript/javascript-the-core/
- https://www.quora.com/What-is-prototypal-inheritance/answer/Kyle-Simpson
- https://davidwalsh.name/javascript-objects
- https://crockford.com/javascript/prototypal.html
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
What do you think of AMD vs CommonJS?
Both are ways to implement a module system, which was not natively present in JavaScript until ES2015 came along. CommonJS is synchronous while AMD (Asynchronous Module Definition) is obviously asynchronous. CommonJS is designed with server-side development in mind while AMD, with its support for asynchronous loading of modules, is more intended for browsers.
I find AMD syntax to be quite verbose and CommonJS is closer to the style you would write import statements in other languages. Most of the time, I find AMD unnecessary, because if you served all your JavaScript into one concatenated bundle file, you wouldn't benefit from the async loading properties. Also, CommonJS syntax is closer to Node style of writing modules and there is less context-switching overhead when switching between client side and server side JavaScript development.
I'm glad that with ES2015 modules, that has support for both synchronous and asynchronous loading, we can finally just stick to one approach. Although it hasn't been fully rolled out in browsers and in Node, we can always use transpilers to convert our code.
References
- https://auth0.com/blog/javascript-module-systems-showdown/
- https://stackoverflow.com/questions/16521471/relation-between-commonjs-amd-and-requirejs
Explain why the following doesn't work as an IIFE: function foo(){ }();
. What needs to be changed to properly make it an IIFE?
IIFE stands for Immediately Invoked Function Expressions. The JavaScript parser reads function foo(){ }();
as function foo(){ }
and ();
, where the former is a function declaration and the latter (a pair of parentheses) is an attempt at calling a function but there is no name specified, hence it throws Uncaught SyntaxError: Unexpected token )
.
Here are two ways to fix it that involves adding more parentheses: (function foo(){ })()
and (function foo(){ }())
. Statements that begin with function
are considered to be function declarations; by wrapping this function within ()
, it becomes a function expression which can then be executed with the subsequent ()
. These functions are not exposed in the global scope and you can even omit its name if you do not need to reference itself within the body.
You might also use void
operator: void function foo(){ }();
. Unfortunately, there is one issue with such approach. The evaluation of given expression is always undefined
, so if your IIFE function returns anything, you can't use it. An example:
const foo = void (function bar() {
return 'foo';
})();
console.log(foo); // undefined
References
- http://lucybain.com/blog/2014/immediately-invoked-function-expression/
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
What's the difference between a variable that is: null
, undefined
or undeclared? How would you go about checking for any of these states?
Undeclared variables are created when you assign a value to an identifier that is not previously created using var
, let
or const
. Undeclared variables will be defined globally, outside of the current scope. In strict mode, a ReferenceError
will be thrown when you try to assign to an undeclared variable. Undeclared variables are bad just like how global variables are bad. Avoid them at all cost! To check for them, wrap its usage in a try
/catch
block.
function foo() {
x = 1; // Throws a ReferenceError in strict mode
}
foo();
console.log(x); // 1
A variable that is undefined
is a variable that has been declared, but not assigned a value. It is of type undefined
. If a function does not return any value as the result of executing it is assigned to a variable, the variable also has the value of undefined
. To check for it, compare using the strict equality (===
) operator or typeof
which will give the 'undefined'
string. Note that you should not be using the abstract equality operator to check, as it will also return true
if the value is null
.
var foo;
console.log(foo); // undefined
console.log(foo === undefined); // true
console.log(typeof foo === 'undefined'); // true
console.log(foo == null); // true. Wrong, don't use this to check!
function bar() {}
var baz = bar();
console.log(baz); // undefined
A variable that is null
will have been explicitly assigned to the null
value. It represents no value and is different from undefined
in the sense that it has been explicitly assigned. To check for null,
simply compare using the strict equality operator. Note that like the above, you should not be using the abstract equality operator (==
) to check, as it will also return true
if the value is undefined
.
var foo = null;
console.log(foo === null); // true
console.log(typeof foo === 'object'); // true
console.log(foo == undefined); // true. Wrong, don't use this to check!
As a personal habit, I never leave my variables undeclared or unassigned. I will explicitly assign null
to them after declaring if I don't intend to use it yet. If you use a linter in your workflow, it will usually also be able to check that you are not referencing undeclared variables.
References
- https://stackoverflow.com/questions/15985875/effect-of-declared-and-undeclared-variables
- https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/undefined
What is a closure, and how/why would you use one?
A closure is the combination of a function and the lexical environment within which that function was declared. The word "lexical" refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available. Closures are functions that have access to the outer (enclosing) function's variables—scope chain even after the outer function has returned.
Why would you use one?
- Data privacy / emulating private methods with closures. Commonly used in the module pattern.
- Partial applications or currying.
References
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
- https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36