JavaScript Interview Questions and Answers.

Md.Saiful Islam
4 min readMay 2, 2020

Here is another 10 important topics on JavaScript interview.Let’s see the outline of this article —

i) Object Oriented Programming (OOP)

ii)Event Loop

iii)Call Stack

iv)Pure Function

v)Impure Function

vi)Higher order function

vii)Immutability

viii)Function Composition

ix)Recursion

x)Currying

Now try to clarify each topic indivadually —

  1. Object Oriented Programming (OOP):

OOP means object based programming. These concepts allow for the creation of Classes and Objects which lead to a greater degree of Abstraction. Three main concepts which make up OOP are: Encapsulation, Polymorphism, and Inheritance.

Encapsulation:

Encapsulation is the combination of data and operations to create a single structure for more abstraction.

Polymorphism:

Poly means multi and morps means forms that multiple forms. So it is the ability of a function to use it for multiple way.

Inheritance:

Inheritance is the concept of an object inheriting properties from another object.

2) Event Loop:

The Event Loop monitors the Call Stack and the Callback Queue. If the Call Stack is empty, it will take the first event from the queue and will push it to the Call Stack, which effectively runs it. Such an iteration is called Event Loop.Each event is just a function callback.

console.log(‘Hi’);setTimeout(function cb1() {console.log(‘cb1’);}, 5000);console.log(‘Bye’);

3) Call Stack:

The Call Stack is a data structure which records basically where in the program we are. If we step into a function, we put it on the top of the stack. If we return from a function, we pop off the top of the stack.

Mainly it follows (LIFO) principle to temporarily store and manage function invocation (call).

Let’s see an example. Take a look at the following code:

function multiply(x, y) {return x * y;}function printSquare(x) {var s = multiply(x, x);console.log(s);}printSquare(5);

When the engine starts executing this code, the Call Stack will be empty. Afterwards, the steps will be the following:

4) Pure Function:

Pure functions are a pillar of functional programming that always returns the same result given same parameters. It doesn’t depend on or can’t modify the state of variables out of its scope.

Let’s see an example —

const obj = {a:5};
function pureFunction(a) {
const c =a+ 2;
return c;
}
Console.log(pureFunction(obj.a)); //output: 7Console.log(pureFunction(5)); //output: 7

5) Impure Function:

It may affected by outside of its lexical scope.

For instance—

const values = { a: 5 }function impureFunction(items) {items.a = items.a + 2return items.a}var c = impureFunction(values)// Now `values.a` is 7, the impure function modifies it.

6) Higher order function:

A function that take a callback function as a parameter is termed as higher order function. There have numerous types of build-in higher order functions in JavaScript like as mas, find, filter, reduce and so on.

Example:

const names = [“rakib”,”sakib”,”rofiq”];const res = names.find(name=>name === “sakib”)? “Yes” : “No”;console.log(res);

7) Immutability:

An immutable object is an object whose state cannot be modified after it is created. For the immutable data types, we have no way of changing the internal state of the data, so the reference always gets reassigned to a new object.

Example:

var person = new ImmutableMap({name: "Chris", age: 32});
var olderPerson = person.set("age", 33);

person.toObject(); // {name: "Chris", age: 32}
olderPerson.toObject(); // {name: "Chris", age: 33}

8) Function Composition:

Function composition is the process of combining two or more functions to produce a new function.

Say for example, if a composition of functions `f` and `g` can be defined as `f(g(x))`, then it executes innermost function first then respectively outer one.so the execution order is —

`x`>`g`>`f`

Here is an example to clearify the fact —

function h(x) {    return x + 1;} //1+1function g(x) {   return x * x;}   //2*2function f(x) {   return Math.pow(x, 3);}   //4^3const y = f(g(h(1)));console.log(y); // 64

9) Recursion:

Recursion is a technique for iterating over an operation by having a function call itself repeatedly until it arrives at a threshold value.

var countdown = function(value) {if (value > 0) {console.log(value);return countdown(value - 1);} else {return value;}};countdown(10);

9) Currying:

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. Here is an Example —

function curry(f) { return function(a) {  return function(b) {    return f(a, b);   }; };}function sum(a, b) { return a + b;}let curriedSum = curry(sum);alert( curriedSum(1)(2) ); // 3

So…end for today.happy learning.

--

--