Most Asked JavaScript Interview Questions - 2
The second part of the JavaScript Series.
Photo by Arnold Francisca on Unsplash
Make sure to go through the first part before jumping into this one: https://amay.hashnode.dev/most-asked-javascript-interview-questions-1
The Rest Operator
The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadic functions in JavaScript.
Read More
Shallow Copy Deep Copy
A deep copy means that all of the values of the new variable are copied and disconnected from the original variable. A shallow copy means that certain (sub-)values are still connected to the original variable. To really understand copying, you have to get into how JavaScript stores values.
Closures
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
Read More
Map vs Reduce vs Filter
Map creates a new array by transforming every element in an array individually. Filter creates a new array by removing elements that don't belong. Reduce, on the other hand, takes all of the elements in an array and reduces them into a single value. Just like map and filter, reduce is defined on Array.
Promise vs Callback
A key difference between the two is when using the callback approach, we'd normally just pass a callback into a function that would then get called upon completion in order to get the result of something. In promises, however, you attach callbacks on the returned promise object.
They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events. In other words also, we may say that, promises are the ideal choice for handling multiple callbacks at the same time, thus avoiding the undesired callback hell situation.
Currying
Currying is an advanced technique of working with functions. It’s used not only in JavaScript, but in other languages as well.
Currying is a transformation of functions that translates a function from callable as f(a, b, c)
into callable as f(a)(b)(c)
. Currying doesn’t call a function. It just transforms it. It is a technique in functional programming, transformation of the function of multiple arguments into several functions of a single argument in sequence.
Read More
Arrow Function
An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.
There are differences between arrow functions and traditional functions, as well as some limitations:
- Arrow functions don't have their own bindings to this, arguments or super, and should not be used as methods.
- Arrow functions don't have access to the new.target keyword.
- Arrow functions aren't suitable for call, apply and bind methods, which generally rely on establishing a scope.
- Arrow functions cannot be used as constructors.
- Arrow functions cannot use yield, within its body.
Callback Function
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
Here is a quick example:
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
The above example is a synchronous callback, as it is executed immediately.
Note, however, that callbacks are often used to continue code execution after an asynchronous operation has completed — these are called asynchronous callbacks. A good example is the callback functions executed inside a .then() block chained onto the end of a promise after that promise fulfills or rejects. This structure is used in many modern web APIs, such as fetch().
Read More
forEach vs Map
A quick tip to Sort in Order using JS
.sort((a,b) ⇒ a - b)
ascending order.sort((a,b) ⇒ b - a)
descending orderAnd that concludes this set. It was a long one but these are very important concepts in JavaScript that every developer should know. Make sure to follow me on Twitter🐦 for more updates. And I think the next series would be on ReactJS.
Did you find this article valuable?
Support Amay Jain by becoming a sponsor. Any amount is appreciated!