window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-63172957-1');
Created by potrace 1.16, written by Peter Selinger 2001-2019
Back to blog

Javascript Interview Questions & Answers (Part 3)

Article index:

What is a pure function?

Pure function is a function where the return value is only determined by its arguments without any side effects. i.e, If you call a function with the same arguments ‘n’ number of times and ‘n’ number of places in the application then it will always return the same value. Let’s take an example to see the difference between pure and impure functions,

let numberArray = [];
const impureAddNumber = number =&amp;gt; numberArray.push (number);
const pureAddNumber = number =&amp;gt; argNumberArray =&amp;gt;
  argNumberArray.concat ([number]);

//Display the results
console.log (impureAddNumber (6)); // returns 6
console.log (numberArray); // returns [6]
console.log (pureAddNumber (7) (numberArray)); // returns [6, 7]
console.log (numberArray); // returns [6]

As per above code snippets, Push function is impure itself by altering the array and returning an push number index which is independent of parameter value. Whereas Concat on the other hand takes the array and concatenates it with the other array producing a whole new array without side effects. Also, the return value is a concatenation of previous array. Remember that Pure functions are important as they simplify unit testing without any side effects and no need for dependency injection. They also avoid tight coupling and makes harder to break your application by not having any side effects. These principles are coming together with Immutability concept of ES6 by giving preference to const over let usage.

What is the purpose of let keyword?

The let statement declares a block scope local variable. Hence the variables defined with let keyword are limited in scope to the block, statement, or expression on which it is used. Whereas variables declared with the var keyword used to define a variable globally, or locally to an entire function regardless of block scope. Let’s take an example to demonstrate the usage,

let counter = 30;
if (counter === 30) {
  let counter = 31;
  console.log(counter); // 31
console.log(counter); // 30 (because if block variable won't exist here)

What is the reason to choose the name let as keyword?

Let is a mathematical statement that was adopted by early programming languages like Scheme and Basic. It has been borrowed from dozens of other languages that use let already as a traditional keyword as close to var as possible.

How do you redeclare variables in switch block without an error?

If you try to redeclare variables in a switch block then it will cause errors because there is only one block. For example, the below code block throws a syntax error as below,

let counter = 1;
switch(x) {
  case 0:
    let name;

  case 1:
    let name; // SyntaxError for redeclaration.
To avoid this error, you can create a nested block inside a case clause will create a new block scoped lexical environment.
let counter = 1;
    switch(x) {
      case 0: {
        let name;
      case 1: {
        let name; // No SyntaxError for redeclaration.

What is Temporal Dead Zone?

The Temporal Dead Zone is a behavior in JavaScript that occurs when declaring a variable with the let and const keywords, but not with var. In ECMAScript 6, accessing a let or const variable before its declaration (within its scope) causes a ReferenceError. The time span when that happens, between the creation of a variable’s binding and its declaration, is called the temporal dead zone. Let’s see this behavior with an example,

function somemethod() {
  console.log(counter1); // undefined
  console.log(counter2); // ReferenceError
  var counter1 = 1;
  let counter2 = 2;

What is IIFE(Immediately Invoked Function Expression)?

IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The signature of it would be as below,

(function ()
      // logic here

The primary reason to use an IIFE is to obtain data privacy because any variables declared within the IIFE cannot be accessed by the outside world. i.e, If you try to access variables with IIFE then it throws an error as below,

(function ()
      var message = &amp;quot;IIFE&amp;quot;;
console.log(message); //Error: message is not defined

What is the benefit of using modules?

There are a lot of benefits to using modules in favour of a sprawling. Some of the benefits are,

What is memoization?

Memoization is a programming technique which attempts to increase a function’s performance by caching its previously computed results. Each time a memoized function is called, its parameters are used to index the cache. If the data is present, then it can be returned, without executing the entire function. Otherwise the function is executed and then the result is added to the cache. Let’s take an example of adding function with memorization,

const memoizAddition = () =&gt; {
  let cache = {};
 return (value) =&gt; {
  if (value in cache) {
   console.log('Fetching from cache');
   return cache[value]; // Here, cache.value cannot be used as property name starts with the number which is not valid JavaScript  identifier. Hence, can only be accessed using the square bracket notation.
  else {
   console.log('Calculating result');
   let result = value + 20;
   cache[value] = result;
   return result;
// returned function from memoizAddition
const addition = memoizAddition();
console.log(addition(20)); //output: 40 calculated
console.log(addition(20)); //output: 40 cached

What are the main rules of promise?

A promise must follow a specific set of rules,

  • A promise is an object that supplies a standard-compliant .then() method
  • A pending promise may transition into either fulfilled or rejected state
  • A fulfilled or rejected promise is settled and it must not transition into any other state.
  • Once a promise is settled, the value must not change.

What is callback in callback?

You can nest one callback inside in another callback to execute the actions sequentially one by one. This is known as callbacks in callbacks.

loadScript('/script1.js', function(script) {
   console.log('first script is loaded');

  loadScript('/script2.js', function(script) {

    console.log('second script is loaded');

    loadScript('/script3.js', function(script) {

        console.log('third script is loaded');
      // after all scripts are loaded



Back to blog

Wordpress Developer Loader, Web Developer Loader , Front End Developer Loader Jack is thinking