Frontend Developer Interview Questions
Frontend Developer Interview Questions
Questions
Prep for the interview and get the job
you want
Alejandro Garcia
This book is for sale at
http://leanpub.com/frontend-developer-interview-questions
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Who is this book for ? . . . . . . . . . . . . . . . . . . . . 10
What this book covers ? . . . . . . . . . . . . . . . . . . . 10
Describe what you like and dislike about the CSS pre-
processors you have used. . . . . . . . . . . . . . . 36
How would you implement a web design comp that uses
non-standard fonts? . . . . . . . . . . . . . . . . . 37
Explain how a browser determines what elements match
a CSS selector. . . . . . . . . . . . . . . . . . . . . 37
Describe pseudo-elements and discuss what they are
used for. . . . . . . . . . . . . . . . . . . . . . . . . 38
Explain your understanding of the box model and how
you would tell the browser in CSS to render your
layout in different box models. . . . . . . . . . . . 39
What does * { box-sizing: border-box; } do? What
are its advantages? . . . . . . . . . . . . . . . . . . 40
What is the CSS display property and can you give a
few examples of its use? . . . . . . . . . . . . . . . 41
What’s the difference between inline and inline-block? 41
What’s the difference between a relative, fixed, abso-
lute and statically positioned element? . . . . . 44
What existing CSS frameworks have you used locally, or
in production? How would you change/improve
them? . . . . . . . . . . . . . . . . . . . . . . . . . 45
Have you played around with the new CSS Flexbox or
Grid specs? . . . . . . . . . . . . . . . . . . . . . . 45
Can you explain the difference between coding a web-
site to be responsive versus using a mobile-first
strategy? . . . . . . . . . . . . . . . . . . . . . . . . 46
How is responsive design different from adaptive design? 47
Have you ever worked with retina graphics? If so, when
and what techniques did you use? . . . . . . . . . 49
Is there any reason you’d want to use translate()
instead of absolute positioning, or vice-versa?
And why? . . . . . . . . . . . . . . . . . . . . . . . 50
Other Books by
Alejandro
Your First Tech Interview: The Questions and Unique Formula to get the Job
you want
Other Books by Alejandro 3
Javascript Challenges
Javascript Challenges⁴
⁴https://leanpub.com/javascript-challenges/?utm_source=ebook&utm_medium=
leanpub&utm_campaign=frontendIQ
Other Books by Alejandro 6
Data Science Workflow for Beginners: Start your Data Science Journey into a
Successful High Paying Career
Tuesday Messages
References
• doctype⁹
• multipage¹⁰
• doctype Multipage¹¹
⁹https://html.spec.whatwg.org/multipage/syntax.html#the-doctype
¹⁰https://html.spec.whatwg.org/multipage/xhtml.html
¹¹https://quirks.spec.whatwg.org/
Chapter 1: HTML Interview Questions 12
In the back end, the HTML markup will contain i18n placeholders
and content for the specific language stored in YML or JSON
formats. The server then dynamically generates the HTML page
with content in that particular language, usually with the help of a
back end framework.
References
• getting Started¹²
• language¹³
¹²https://www.w3.org/International/getting-started/language
¹³https://support.google.com/webmasters/answer/189077
Chapter 1: HTML Interview Questions 13
References
• developing multilingual¹⁴
References
• custom attributes¹⁵
• non visible data¹⁶
References
• html5¹⁷
¹⁵http://html5doctor.com/html5-custom-data-attributes/
¹⁶https://www.w3.org/TR/html5/dom.html#embedding-custom-non-visible-data-with-
the-data-*-attributes
¹⁷https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5
Chapter 1: HTML Interview Questions 16
References
• cookies¹⁸
• local and session storage¹⁹
Note: The async and defer attributes are ignored for scripts that
have no src attribute.
¹⁸https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
¹⁹http://tutorial.techaltum.com/local-and-session-storage.html
Chapter 1: HTML Interview Questions 18
References
• async²⁰
• script tag async²¹
• async vs defer²²
<script> tags block HTML parsing while they are being down-
loaded and executed which can slow down your page. Placing
the scripts at the bottom will allow the HTML to be parsed and
displayed to the user first.
An exception for positioning of <script>s at the bottom is when
your script contains document.write(), but these days it’s not a
good practice to use document.write(). Also, placing <script>s
at the bottom means that the browser cannot start downloading
the scripts until the entire document is parsed. This ensures your
code that needs to manipulate DOM elements will not throw and
error and halt the entire script. If you need to put <script> in the
<head>, use the defer attribute, which will achieve the same effect
of downloading and running the script only after the HTML is
parsed.
Keep in mind that putting scripts just before the closing </body>
tag will create the illusion that the page loads faster on an empty
cache (since the scripts won’t block downloading the rest of the
document). However, if you have some code you want to run
during page load, it will only start executing after the entire page
has loaded. If you put those scripts in the <head> tag, they would
start executing before - so on a primed cache the page would
actually appear to load faster.
References
• rules²³
• prevent unstyled²⁴
• critical rendering²⁵
²³https://developer.yahoo.com/performance/rules.html#css_top
²⁴https://www.techrepublic.com/blog/web-designer/how-to-prevent-flash-of-unstyled-
content-on-your-websites/
²⁵https://developers.google.com/web/fundamentals/performance/critical-rendering-path/
Chapter 1: HTML Interview Questions 20
References
• progressive rendering²⁷
• rendering with marko²⁸
²⁶http://www.ebaytechblog.com/2014/12/08/async-fragments-rediscovering-progressive-
html-rendering-with-marko/
²⁷https://stackoverflow.com/questions/33651166/what-is-progressive-rendering
²⁸http://www.ebaytechblog.com/2014/12/08/async-fragments-rediscovering-progressive-
html-rendering-with-marko/
Chapter 1: HTML Interview Questions 21
References
• responsive images²⁹
• srcset³⁰
internal or external) twice, then the lower rule in your style sheet
is closer to the element to be styled, it is deemed to be more specific
and therefore will be applied.
I would write CSS rules with low specificity so that they can be
easily overridden if necessary. When writing CSS UI component
library code, it is important that they have low specificities so
that users of the library can override them without using too
complicated CSS rules just for the sake of increasing specificity or
resorting to !important.
References
• css specificity³¹
• specificity³²
References
• normalize css³³
1 .clearfix:after {
2 content: ' ';
3 visibility: hidden;
4 display: block;
5 height: 0;
6 clear: both;
7 }
³³https://stackoverflow.com/questions/6887336/what-is-the-difference-between-
normalize-css-and-reset-css
Chapter 2: CSS Interview Questions 26
References
• floats³⁴
Without any z-index value, elements stack in the order that they
appear in the DOM (the lowest one down at the same hierarchy
level appears on top). Elements with non-static positioning (and
their children) will always appear on top of elements with default
static positioning, regardless of HTML hierarchy.
A stacking context is an element that contains a set of layers.
Within a local stacking context, the z-index values of its children
are set relative to that element rather than to the document root.
Layers outside of that context — i.e. sibling elements of a local
stacking context — can’t sit between layers within it. If an element
B sits on top of element A, a child element of element A, element C,
can never be higher than element B even if element C has a higher
z-index than element B.
References
• z index³⁶
• z-index³⁷
• understand z index³⁸
In a BFC, each box’s left outer edge touches the left edge of the
containing block (for right-to-left formatting, right edges touch).
Vertical margins between adjacent block-level boxes in a BFC
collapse. Read more on collapsing margins⁴⁰.
References
• block formatting⁴¹
• formating contexts⁴²
1. Use a sprite generator that packs multiple images into one and
generate the appropriate CSS for it.
2. Each image would have a corresponding CSS class with
background-image, background-position and background-size
properties defined.
3. To use that image, add the corresponding class to your
element.
Advantages:
References
• css sprites⁴³
⁴³https://css-tricks.com/css-sprites/
Chapter 2: CSS Interview Questions 30
References
• wai aria⁴⁷
• accesibility⁴⁸
• a11project⁴⁹
⁴⁵https://modernizr.com/
⁴⁶https://developer.mozilla.org/en-US/docs/Web/CSS/@supports
⁴⁷https://www.w3.org/TR/wai-aria-1.1/
⁴⁸https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA
⁴⁹http://a11yproject.com/
Chapter 2: CSS Interview Questions 32
1 <rect
2 x="10"
3 y="10"
4 width="100"
5 height="100"
6 stroke="blue"
7 fill="purple"
8 fill-opacity="0.5"
9 stroke-opacity="0.8"
10 />
References
1 @media print {
2 body {
3 color: black;
4 }
5 }
References
• media syntax⁵⁴
the faster the browser can determine if that element matches the
selector. Hence avoid key selectors that are tag and universal
selectors. They match a large number of elements and browsers
will have to do more work in determining if the parents do match.
BEM Block Element Modifier⁵⁵ methodology recommends that
everything has a single class, and, where you need hierarchy, that
gets baked into the name of the class as well, this naturally makes
the selector efficient and easy to override.
Be aware of which CSS properties trigger⁵⁶ reflow, repaint, and
compositing. Avoid writing styles that change the layout (trigger
reflow) where possible.
References
• rendering⁵⁷
• csstriggers⁵⁸
Disadvantages:
Dislikes:
⁵⁹https://github.com/postcss/postcss-loader
⁶⁰https://webpack.js.org/
Chapter 2: CSS Interview Questions 37
References
• css selectors⁶¹
References
References
References
• box sizing⁶⁴
⁶³https://www.smashingmagazine.com/2010/06/the-principles-of-cross-browser-css-
coding/#understand-the-css-box-model
⁶⁴https://www.paulirish.com/2012/box-sizing-border-box-ftw/
Chapter 2: CSS Interview Questions 41
display Description
none Does not display an element (the
elementv no longer affects the layout
of the document). All child element are
also no longer displayed. The
document is rendered as if the element
did not exist in the document tree
block The element consumes the whole line
in the block direction (which is usually
horizontal)
inline Elements can be laid out beside each
other
inline-block Similar to inline, but allows some
block properties like setting width and
height
table Behaves like the <table> element
table-row Behaves like the <tr> element
table-cell Behaves like the <td> element
list-item Behaves like a <li> element which
allows it to define list-style-type
and list-style-position
• static - The default position; the element will flow into the
page as it normally would. The top, right, bottom, left and
z-index properties do not apply.
• relative - The element’s position is adjusted relative to itself,
without changing layout (and thus leaving a gap for the
element where it would have been had it not been positioned).
• absolute - The element is removed from the flow of the page
and positioned at a specified position relative to its closest
positioned ancestor if any, or otherwise relative to the initial
containing block. Absolutely positioned boxes can have mar-
gins, and they do not collapse with any other margins. These
elements do not affect the position of other elements.
• fixed - The element is removed from the flow of the page and
positioned at a specified position relative to the viewport and
doesn’t move when scrolled.
• sticky - Sticky positioning is a hybrid of relative and fixed
positioning. The element is treated as relative positioned
until it crosses a specified threshold, at which point it is
treated as fixed positioned.
References
• position⁶⁵
⁶⁵https://developer.mozilla.org/en/docs/Web/CSS/position
Chapter 2: CSS Interview Questions 45
References
• flexbox⁶⁶
10 }
11 }
1 .my-class {
2 font-size: 12px;
3 }
4
5 @media (min-width: 600px) {
6 .my-class {
7 font-size: 24px;
8 }
9 }
References
• adaptive design⁶⁷
• responsive adaptive⁶⁸
• responsive vs adaptive⁶⁹
⁶⁷https://developer.mozilla.org/en-US/docs/Archive/Apps/Design/UI_layout_basics/
Responsive_design_versus_adaptive_design
⁶⁸http://mediumwell.com/responsive-adaptive-mobile/
⁶⁹https://css-tricks.com/the-difference-between-responsive-and-adaptive-design/
Chapter 2: CSS Interview Questions 49
1 <div responsive-background-image>
2 <img
3 src="/images/test-1600.jpg"
4 sizes="
5 (min-width: 768px) 50vw,
6 (min-width: 1024px) 66vw,
7 100vw"
8 srcset="
9 /images/test-400.jpg 400w,
10 /images/test-800.jpg 800w,
Chapter 2: CSS Interview Questions 50
11 /images/test-1200.jpg 1200w
12 "
13 />
14 </div>
References
• responsive images⁷⁰
• picturefill⁷¹
• responsive background⁷²
References
• moving elements⁷³
⁷³https://www.paulirish.com/2012/why-moving-elements-with-translate-is-better-than-
posabs-topleft/
Chapter 3: Javascript
Interview Questions
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:
References
• event delegate⁷⁴
• dom delegation⁷⁵
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:
ES6 allows you to use arrow functions⁷⁸ which uses the enclosing
lexical scope⁷⁹. This is usually convenient, but does prevent the
⁷⁶https://medium.com/@arnav_aggarwal
⁷⁷https://codeburst.io/the-simple-rules-to-this-in-javascript-35d97f31bde3
⁷⁸http://2ality.com/2017/12/alternate-this.html#arrow-functions
⁷⁹https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_
functions#No_separate_this
Chapter 3: Javascript Interview Questions 54
References
• simple rules⁸⁰
• es6⁸¹
⁸⁰https://codeburst.io/the-simple-rules-to-this-in-javascript-35d97f31bde3
⁸¹https://stackoverflow.com/a/3127440/1751946
⁸²https://davidwalsh.name/javascript-objects
Chapter 3: Javascript Interview Questions 55
– Object.create(Parent.prototype);
– Object.create(new Parent(null));
– Object.create(objLiteral);
– Currently, child.constructor is pointing to the Parent:
1 child.constructor
2 ƒ () {
3 this.name = "Parent";
4 }
5 child.constructor.name
6 "Parent"
1 function Child() {
2 Parent.call(this);
3 this.name = 'child';
4 }
5
6 Child.prototype = Parent.prototype;
7 Child.prototype.constructor = Child;
8
9 const c = new Child();
10
11 c.cry();
12 // Outputs: waaaaaahhhh!
13
14 c.greet();
15 // Outputs: hello from Parent
16
17 c.constructor.name;
18 // Outputs: "Child"
Chapter 3: Javascript Interview Questions 57
References
References
• module systems⁸⁵
• commonjs⁸⁶
Here are two ways to fix it that involves adding more paren-
theses: (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 subse-
quent (). 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(){ }();. Un-
fortunately, 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:
⁸⁵https://auth0.com/blog/javascript-module-systems-showdown/
⁸⁶https://stackoverflow.com/questions/16521471/relation-between-commonjs-amd-and-
requirejs
Chapter 3: Javascript Interview Questions 59
References
• iife⁸⁷
• void⁸⁸
⁸⁷http://lucybain.com/blog/2014/immediately-invoked-function-expression/
⁸⁸https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
Chapter 3: Javascript Interview Questions 60
1 function foo() {
2 x = 1; // Throws a ReferenceError in strict mode
3 }
4
5 foo();
6 console.log(x); // 1
1 var foo;
2 console.log(foo); // undefined
3 console.log(foo === undefined); // true
4 console.log(typeof foo === 'undefined'); // true
5
6 console.log(foo == null); // true. Wrong, don't use this \
7 to check!
8
9 function bar() {}
10 var baz = bar();
11 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.
Chapter 3: Javascript Interview Questions 61
References
• variables⁸⁹
• global objects⁹⁰
References
• closures⁹³
• what is a closure⁹⁴
⁹¹https://addyosmani.com/resources/essentialjsdesignpatterns/book/
#modulepatternjavascript
⁹²https://medium.com/javascript-scene/curry-or-partial-application-8150044c78b8#
.l4b6l1i3x
⁹³https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
⁹⁴https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-
closure-b2f0d2152b36
Chapter 3: Javascript Interview Questions 63
map
References
• map vs foreach⁹⁵
⁹⁵https://codeburst.io/javascript-map-vs-foreach-f38111822c0f
Chapter 3: Javascript Interview Questions 64
1 (function () {
2 // Some code here.
3 })();
As a callback that is used once and does not need to be used any-
where else. The code will seem more self-contained and readable
when handlers are defined right inside the code calling them, rather
than having to search elsewhere to find the function body.
1 setTimeout(function () {
2 console.log('Hello world!');
3 }, 1000);
References
• anonymous functions⁹⁶
• anonymous⁹⁷
⁹⁶https://www.quora.com/What-is-a-typical-usecase-for-anonymous-functions
⁹⁷https://stackoverflow.com/questions/10273185/what-are-the-benefits-to-using-
anonymous-functions-instead-of-named-functions-fo
Chapter 3: Javascript Interview Questions 65
References
⁹⁹https://stackoverflow.com/questions/7614317/what-is-the-difference-between-native-
objects-and-host-objects
Chapter 3: Javascript Interview Questions 67
1 function Person(name) {
2 this.name = name;
3 }
4
5 var person = Person('John');
6 console.log(person); // undefined
7 console.log(person.name); // Uncaught TypeError: Cannot r\
8 ead property 'name' of undefined
9
10 var person = new Person('John');
11 console.log(person); // Person { name: "John" }
12 console.log(person.name); // "john"
References
• operators¹⁰⁰
¹⁰⁰https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new
Chapter 3: Javascript Interview Questions 68
1 function add(a, b) {
2 return a + b;
3 }
4
5 console.log(add.call(null, 1, 2)); // 3
6 console.log(add.apply(null, [1, 2])); // 3
Explain Function.prototype.bind.
Taken word-for-word from MDN¹⁰¹:
References
• bind¹⁰²
after the page has loaded, it will call document.open which clears
the whole document (<head> and <body> removed!) and replaces
the contents with the given parameter value. Hence it is usually
considered dangerous and prone to misuse.
There are some answers online that explain document.write() is
being used in analytics code or when you want to include styles
that should only work if JavaScript is enabled¹⁰³. It is even being
used in HTML5 boilerplate to load scripts in parallel and preserve
execution order¹⁰⁴! However, I suspect those reasons might be
outdated and in the modern day, they can be achieved without
using document.write(). Please do correct me if I’m wrong about
this.
References
• doc write¹⁰⁵
• script loading¹⁰⁶
1 if ('geolocation' in navigator) {
2 // Can use navigator.geolocation
3 } else {
4 // Handle lack of feature
5 }
1 if (document.getElementsByTagName) {
2 element = document.getElementById(id);
3 }
References
• feature detection¹⁰⁸
• user agent¹⁰⁹
References
• ajax¹¹⁰
• ajax docs¹¹¹
¹⁰⁸https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_
testing/Feature_detection
¹⁰⁹https://stackoverflow.com/questions/20104930/whats-the-difference-between-feature-
detection-feature-inference-and-using-th
¹¹⁰https://en.wikipedia.org/wiki/Ajax_(programming)
¹¹¹https://developer.mozilla.org/en-US/docs/AJAX
Chapter 3: Javascript Interview Questions 72
Disadvantages
The client has to have the printData function in its global scope
and the function will be executed by the client when the response
from the cross-origin domain is received.
JSONP can be unsafe and has some security implications. As JSONP
is really JavaScript, it can do everything else JavaScript can do, so
you need to trust the provider of the JSONP data.
These days, CORS¹¹² is the recommended approach and JSONP is
seen as a hack.
References
• cross origin¹¹³
¹¹²http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
¹¹³https://stackoverflow.com/a/2067584/1751946
Chapter 3: Javascript Interview Questions 74
Explain “hoisting”.
Hoisting is a term used to explain the behavior of variable dec-
larations in your code. Variables declared or initialized with the
var keyword will have their declaration “moved” up to the top of
their module/function-level scope, which we refer to as hoisting.
However, only the declaration is hoisted, the assignment (if there
is one), will stay where it is.
Note that the declaration is not actually moved - the JavaScript
engine parses the declarations during compilation and becomes
aware of declarations and their scopes. It is just easier to understand
this behavior by visualizing the declarations as being hoisted to the
top of their scope. Let’s explain with a few examples.
Chapter 3: Javascript Interview Questions 75
1 console.log(foo); // undefined
2 var foo = 1;
3 console.log(foo); // 1
1 // Function Declaration
2 console.log(foo); // [Function: foo]
3 foo(); // 'FOOOOO'
4 function foo() {
5 console.log('FOOOOO');
6 }
7 console.log(foo); // [Function: foo]
8
9 // Function Expression
10 console.log(bar); // undefined
11 bar(); // Uncaught TypeError: bar is not a function
12 var bar = function () {
13 console.log('BARRRR');
14 };
15 console.log(bar); // [Function: bar]
Variables declared via let and const are hoisted as well. However,
unlike var and function, they are not initialized and accessing them
before the declaration will result in a ReferenceError exception.
The variable is in a “temporal dead zone” from the start of the block
until the declaration is processed.
Chapter 3: Javascript Interview Questions 76
1 x; // undefined
2 y; // Reference error: y is not defined
3
4 var x = 'local';
5 let y = 'local';
References
• variable hosting¹¹⁴
• let or const¹¹⁵
¹¹⁴https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_Types#
Variable_hoisting
¹¹⁵https://stackoverflow.com/questions/31219420/are-variables-declared-with-let-or-
const-not-hoisted-in-es6/31222689#31222689
Chapter 3: Javascript Interview Questions 77
But after you change the value of the text field by adding “World!”
to it, this becomes:
1 console.log(input.getAttribute('value')); // Hello
2 console.log(input.value); // Hello World!
References
References
• extend ojects¹¹⁷
References
• DOMContentLoaded¹¹⁸
• load¹¹⁹
1 1 == '1'; // true
2 1 == [1]; // true
3 1 == true; // true
4 0 == ''; // true
5 0 == '0'; // true
6 0 == false; // true
1 var a = null;
2 console.log(a == null); // true
3 console.log(a == undefined); // true
References
• equals operator¹²⁰
References
1 function duplicate(arr) {
2 return arr.concat(arr);
3 }
4
5 duplicate([1, 2, 3, 4, 5]); // [1,2,3,4,5,1,2,3,4,5]
Or with ES6:
References
• conditional operator¹²²
Disadvantages:
References
• strict mode¹²³
• js use strict¹²⁴
References
• loop¹²⁶
The DOM event DOMContentLoaded will fire after the DOM for the
page has been constructed, but do not wait for other resources to
finish loading. This is preferred in certain cases when you do not
need the full page to be loaded before initializing.
TODO.
References
• onload¹²⁷
• The app feels more responsive and users do not see the flash
between page navigations due to full-page refreshes.
• Fewer HTTP requests are made to the server, as the same
assets do not have to be downloaded again for each page load.
• Clear separation of the concerns between the client and the
server; you can easily build new clients for different platforms
(e.g. mobile, chatbots, smart watches) without having to
modify the server code. You can also modify the technology
stack on the client and server independently, as long as the
API contract is not broken.
The downsides:
References
References
• promise¹³⁴
¹³¹https://prerender.io/
¹³²https://github.com/grab/front-end-guide#single-page-apps-spas
¹³³http://stackoverflow.com/questions/21862054/single-page-app-advantages-and-
disadvantages
¹³⁴https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-
promise-27fc71e77261
Chapter 3: Javascript Interview Questions 87
Cons
References
• async performance¹³⁵
¹³⁵https://github.com/getify/You-Dont-Know-JS/blob/master/async%20%26%
20performance/ch3.md
Chapter 3: Javascript Interview Questions 88
Disadvantages:
References
• coffee script¹³⁶
References
• devtools¹⁴¹
• debugging¹⁴²
For arrays:
Most of the time, I would prefer the .forEach method, but it really
depends on what you are trying to do. Before ES6, we used for loops
when we needed to prematurely terminate the loop using break.
But now with ES6, we can do that with for-of loops. I would use
for loops when I need even more flexibility, such as incrementing
the iterator more than once per loop.
Also, when using the for-of loop, if you need to access both the
index and value of each array element, you can do so with the ES6
Array entries() method and destructuring:
References
• foreach¹⁴⁴
• array¹⁴⁵
¹⁴⁴http://2ality.com/2015/08/getting-started-es6.html#from-for-to-foreach-to-for-of
¹⁴⁵https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/
Array/entries
Chapter 3: Javascript Interview Questions 93
Prevent Extensions
If you want to prevent an object from having new properties added
to it, but otherwise leave the rest of the object’s properties alone,
call Object.preventExtensions(...):
1 var myObject = {
2 a: 2,
3 };
4
5 Object.preventExtensions(myObject);
6
7 myObject.b = 3;
8 myObject.b; // undefined
So, not only can you not add any more properties, but you also
cannot reconfigure or delete any existing properties (though you
can still modify their values).
Chapter 3: Javascript Interview Questions 94
Freeze
Object.freeze() creates a frozen object, which means it takes an
existing object and essentially calls Object.seal() on it, but it also
marks all “data accessor” properties as writable:false, so that their
values cannot be changed.
This approach is the highest level of immutability that you can
attain for an object itself, as it prevents any changes to the object
or to any of its direct properties (though, as mentioned above, the
contents of any referenced other objects are unaffected).
Cons
References
• immutability¹⁴⁶
¹⁴⁶https://stackoverflow.com/questions/1863515/pros-cons-of-immutability-vs-mutability
Chapter 3: Javascript Interview Questions 96
1 // Array Example
2 const arr = [1, 2, 3];
3 const newArr = [...arr, 4]; // [1, 2, 3, 4]
4
5 // Object Example
6 const human = Object.freeze({race: 'human'});
7 const john = {...human, name: 'John'}; // {race: "human",\
8 name: "John"}
9 const alienJohn = {...john, race: 'alien'}; // {race: "al\
10 ien", name: "John"}
References
References
• event loop¹⁵³
• event loop stack¹⁵⁴
Function Declaration
1 foo(); // 'FOOOOO'
2 function foo() {
3 console.log('FOOOOO');
4 }
Function Expression
¹⁵²https://2014.jsconf.eu/speakers/philip-roberts-what-the-heck-is-the-event-loop-
anyway.html
¹⁵³https://2014.jsconf.eu/speakers/philip-roberts-what-the-heck-is-the-event-loop-
anyway.html
¹⁵⁴http://theproactiveprogrammer.com/javascript/the-javascript-event-loop-a-stack-and-
a-queue/
Chapter 3: Javascript Interview Questions 99
References
• function¹⁵⁵
Variables declared using the var keyword are scoped to the function
in which they are created, or if created outside of any function, to
the global object. let and const are block scoped, meaning they
are only accessible within the nearest set of curly braces (function,
if-else block, or for-loop).
1 function foo() {
2 // All variables are accessible within functions.
3 var bar = 'bar';
4 let baz = 'baz';
5 const qux = 'qux';
6
7 console.log(bar); // bar
8 console.log(baz); // baz
9 console.log(qux); // qux
10 }
11
¹⁵⁵https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/
function
Chapter 3: Javascript Interview Questions 100
1 if (true) {
2 var bar = 'bar';
3 let baz = 'baz';
4 const qux = 'qux';
5 }
6
7 // var declared variables are accessible anywhere in the \
8 function scope.
9 console.log(bar); // bar
10 // let and const defined variables are not accessible out\
11 side of the block they were defined in.
12 console.log(baz); // ReferenceError: baz is not defined
13 console.log(qux); // ReferenceError: qux is not defined
1 console.log(foo); // undefined
2
3 var foo = 'foo';
4
5 console.log(baz); // ReferenceError: can't access lexical\
6 declaration 'baz' before initialization
7
8 let baz = 'baz';
9
10 console.log(bar); // ReferenceError: can't access lexical\
11 declaration 'bar' before initialization
12
13 const bar = 'bar';
Chapter 3: Javascript Interview Questions 101
Redeclaring a variable with var will not throw an error, but let and
const will.
let and const differ in that let allows reassigning the variable’s
value while const does not.
1 // This is fine.
2 let foo = 'foo';
3 foo = 'bar';
4
5 // This causes an exception.
6 const baz = 'baz';
7 baz = 'qux';
References
• let¹⁵⁶
• var¹⁵⁷
¹⁵⁶https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
¹⁵⁷https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
Chapter 3: Javascript Interview Questions 102
It’s much more verbose to use inheritance in ES5 and the ES6
version is easier to understand and remember.
References
• inheritance¹⁵⁸
• classical inheritance¹⁵⁹
¹⁵⁸https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance
¹⁵⁹https://eli.thegreenplace.net/2013/10/22/classical-inheritance-in-javascript-es5
Chapter 3: Javascript Interview Questions 104
36 sayNameFromWindow2(); // John
The main takeaway here is that this can be changed for a normal
function, but the context always stays the same for an arrow
function. So even if you are passing around your arrow function
to different parts of your application, you wouldn’t have to worry
about the context changing.
This can be particularly helpful in React class components. If you
define a class method for something such as a click handler using
a normal function, and then you pass that click handler down into
a child component as a prop, you will need to also bind this in the
constructor of the parent component. If you instead use an arrow
function, there is no need to also bind “this”, as the method will
automatically get its “this” value from its enclosing lexical context.
See this article for an excellent demonstration and sample code:
Handle Events in React¹⁶⁰
References
• arrow functions¹⁶¹
• react arrow functions¹⁶²
References
1 // Variable assignment.
2 const foo = ['one', 'two', 'three'];
3
4 const [one, two, three] = foo;
5 console.log(one); // "one"
6 console.log(two); // "two"
7 console.log(three); // "three"
¹⁶³https://medium.com/javascript-scene/higher-order-functions-composing-software-
5365cf2cbe99
¹⁶⁴https://hackernoon.com/effective-functional-javascript-first-class-and-higher-order-
functions-713fde8df50a
Chapter 3: Javascript Interview Questions 109
1 // Swapping variables
2 let a = 1;
3 let b = 3;
4
5 [a, b] = [b, a];
6 console.log(a); // 3
7 console.log(b); // 1
Object destructuring
1 // Variable assignment.
2 const o = {p: 42, q: true};
3 const {p, q} = o;
4
5 console.log(p); // 42
6 console.log(q); // true
References
• destructuring¹⁶⁵
• es6 destructure¹⁶⁶
With template literals, you can now create that same output like
this instead:
Note that you use backticks, not quotes, to indicate that you are
using a template literal and that you can insert expressions inside
the ${} placeholders.
A second helpful use case is in creating multi-line strings. Before
ES2015, you could create a multi-line string like this:
References
• template literals¹⁶⁷
1 function curry(fn) {
2 if (fn.length === 0) {
3 return fn;
4 }
5
6 function _curried(depth, args) {
7 return function (newArgument) {
8 if (depth - 1 === 0) {
9 return fn(...args, newArgument);
10 }
11 return _curried(depth - 1, [...args, newArgument]);
12 };
13 }
14
15 return _curried(fn.length, []);
16 }
17
18 function add(a, b) {
19 return a + b;
20 }
21
22 var curriedAdd = curry(add);
23 var addFive = curriedAdd(5);
24
25 var result = [0, 1, 2, 3, 4, 5].map(addFive); // [5, 6, 7\
26 , 8, 9, 10]
Chapter 3: Javascript Interview Questions 113
References
• currying¹⁶⁸
1 function putDookieInAnyArray(arr) {
2 return [...arr, 'dookie'];
3 }
4
5 const result = putDookieInAnyArray(['I', 'really', "don't\
6 ", 'like']); // ["I", "really", "don't", "like", "dookie"\
7 ]
8
9 const person = {
10 name: 'Todd',
11 age: 29,
12 };
13
14 const copyOfTodd = {...person};
1 function addFiveToABunchOfNumbers(...numbers) {
2 return numbers.map((x) => x + 5);
3 }
4
5 const result = addFiveToABunchOfNumbers(4, 5, 6, 7, 8, 9,\
6 10); // [9, 10, 11, 12, 13, 14, 15]
7
8 const [a, b, ...rest] = [1, 2, 3, 4]; // a: 1, b: 2, rest\
9 : [3, 4]
10
11 const {e, f, ...others} = {
12 e: 1,
13 f: 2,
14 g: 3,
15 h: 4,
16 }; // e: 1, f: 2, others: { g: 3, h: 4 }
References
• spread syntax¹⁶⁹
• rest parameters¹⁷⁰
References
• whyamd¹⁷¹
• moudles¹⁷²
References
• static variable methods¹⁷³
¹⁷¹http://requirejs.org/docs/whyamd.html
¹⁷²https://nodejs.org/docs/latest/api/modules.html
¹⁷³https://stackoverflow.com/questions/21155438/when-to-use-static-variables-methods-
and-when-to-use-instance-variables-methods
Keep developing your
interviewing skills
This book is just a starting point for you to get inspired to keep
preparing for tech and frontend development interviews.
The more questions and answers you can prep the more confidence
you will build, this will have 2 positive effects: An incredible
appetite to take more questions, and a positive and energyzed
attitude during your interview which increases the chances of
landing the job.
Also remember, the more courses you can take on technologies and
frameworks to develop great frontend applications the better.
I want to say thanks for taking your time and reading the whole
book, I really appreciate your commitment towards your education
and professional success, and for that I’d like to point you in the
right direction with the following incredible resources which will
help you achieve your goals.
I hope this book was useful, please feel free to contact me with
any questions. Also I would appreciate your honest review or any
feedback to improve the next editions of the book.
Thanks a lot for following along !!
¹⁷⁴https://coursesim.com
¹⁷⁵https://www.facebook.com/theproghub
¹⁷⁶https://www.quora.com/q/vlyubtzhdhpacqzr?invite_code=Yj9D6Z7H2Bf4CONidZmD
About the Author
Alejandro is a programmer and writer with the goal of inspiring
people to learn about several tech related topics.
He’s also a Udemy Instructor here’s a link to his profile: Alejandro
Garcia Udemy Instructor¹⁷⁷
Copyright
¹⁷⁷https://www.udemy.com/user/alejandro-garcia-172
¹⁷⁸https://www.amazon.com/Alejandro-Garcia/e/B08CF2H6TB
¹⁷⁹https://leanpub.com/u/alejandro-garcia
¹⁸⁰https://github.com/yangshun/front-end-interview-handbook/blob/master/LICENSE