The difference between coding JavaScript coding questions vs algorithmic coding questions is that the former is usually specific to the front end domain and it makes the most sense to use JavaScript (or TypeScript) to complete them. You will also probably need to make use of browser/JavaScript-specific APIs and/or utilize HTML/CSS/JavaScript knowledge.
These JavaScript coding questions tend to be practical in nature and can come in one or more of the following categories:
It may seem redundant to implement standard classes/methods when they are already part of the language. However, browsers inconsistencies used to be a rampant problem and some language APIs are not found in older browsers. Hence developers had to resort to polyfilling, the act of providing modern functionality on older browsers that do not natively support it by implementing these APIs in downloaded JavaScript. Being able to implement these native functions also shows good understanding of front end fundamentals.
Array
methods: Array.prototype.map
, Array.prototype.reduce
, Array.prototype.filter
.Promise
and other Promise
-related functions: Promise.all
, Promise.any
.document.getElementsByTagName
, document.getElementsByClassName
.There's more to these functions than meets the eye. Let's take the innocent Array.prototype.map
for example. Are you aware that:
index
and this
?[1, 2, , 4].map(val => val * val) === [1, 4, , 16]
.map
is set before the first call to callbackfn. Elements which are appended to the array after the call to map
begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callbackfn will be the value at the time map
visits them; elements that are deleted after the call to map
begins and before being visited are not visited. Source: Array.prototype.map
ECMAScript specificationYour implementation doesn't have to handle all of these cases, especially the array mutation one. However, it's a positive signal if you mentioned these cases. The closer your implementation is to the specification, the more senior/experienced you will appear to be.
These functions/classes are commonly needed when building software with JavaScript but aren't in the standard language (for now).
debounce
, throttle
, flatten
, curry
, cloneDeep
.jQuery.css
, jQuery.toggleClass
.classnames
test
/expect
functions from testing frameworks like Jest/MochaEmitter
(which exists as part of Node.js and many third party libraries)If you take a look at the source code of these libraries, you might find some of the implementation to be quite complex. This is because there are lots of obscure real world use cases that the library has to support. Similar to the standard functions, you're not expected to handle all of these edge cases within an interview setting but you get points for calling them out.
JavaScript coding interviews share a lot of similarities with algorithmic coding interviews. In general, you should:
Category | Important Topics |
---|---|
Data Structures | Arrays, Maps, Stacks, Trees, Sets |
Algorithms | Binary Search, Breadth-first Search, Depth-first Search, Recursion |
JavaScript Language | Data types (checking for types, type coercion), Scope, Closures, Callbacks, How this keyword works, Object-oriented Programming in JavaScript (prototypes, classes, methods), Arrow functions vs normal functions, Invoking functions via Function.prototype.apply() /Function.prototype.call() , Promise , Handling variadic arguments |
DOM | DOM traversal, DOM creation, DOM manipulation, Accessing element/node properties, Event delegation |
Runtime APIs | Timer (setTimeout() , setInterval() ) |
JavaScript coding interviews are similar to algorithmic coding interviews and the way to go about the interviews should be similar. Naturally, there will be some overlaps with algorithmic coding interviews regarding how candidates are evaluated during JavaScript coding interviews.
this
matters. If a function accepts a callback function as a parameter, consider how the this
variable should behave. For many built-in functions, this
is provided as one of the arguments the callback function is invoked with.From experience, the best JavaScript coding interview questions to practice, as determined by frequency and important concepts covered are:
GreatFrontEnd has a comprehensive list of JavaScript coding questions that you can practice. There are also automated tests cases you can run your code against to verify the correctness and solutions written by ex-FAANG Senior Engineers.
Note that we are intentionally vague in some of the questions and don't present the full requirements upfront in the question description. However, we'll cover as much ground as possible in the solution. It may be frustrating while reading the solutions to see that you've missed out some things, but this trains you to think ahead and consider what are the possible areas you have to take note of when working on the solution. Better to find out during practice than during actual interviews.