Advanced JavaScript interview questions.

Any web development interview will not be completed without JavaScript questions. JavaScript is now one of the most used scripting languages in the World. It is heavily used in the front-end and nowadays in the backend (Node.js). The introduction of ES6 added more momentum to JavaScript.

ReactJS getting immense popularity for building reactive applications. So JavaScript is in a booming phase. Any web developer should have a basic understanding of JavaScript.

The concept of a full-stack developer includes major parts of JavaScript. If someone claims to be a full-stack developer, he or she must have a clear understanding of JavaScript. For web developers, JavaScript skill is a must!

Following are some advanced JavaScript interview questions:

How the event loop works in JavaScript?

How to check equality for two JavaScript objects?

Works when you have simple JSON-style objects without methods and DOM nodes inside:

JSON.stringify(object1) === JSON.stringify(object2) 


const obj1 = {a: 1, b: 2}; const obj2 = {b: 2, a: 1};
JSON.stringify(obj1) === JSON.stringify(obj2) // false


const obj1 = {a: 1, b: 2}; const obj2 = {a: 1, b: 2};
JSON.stringify(obj1) === JSON.stringify(obj2) // true

Lodash has a function called isEqual().

_.isEqual(object1, object2);

How do I check if an array includes a value in JavaScript?

ECMAScript 7 introduces Array#includes, it checks an item included inside an array or not. It is widely supported by all browsers except IE:

// Approach #1:
console.log(['BMW', 'Toyota', 'Audi'].includes('BMW')); // true

// Approach #2:
console.log(['BMW', 'Toyota', 'Audi'].indexOf('Audi') >= 0); // true

// Approach #3:
console.log(['BMW', 'Toyota', 'Audi'].lastIndexOf('Audi') >= 0); // true

// Approach #4:
console.log(['BMW', 'Toyota', 'Audi'].some(item => item === 'Audi')); // true

// Approach #5:
console.log(['BMW', 'Toyota', 'Audi'].filter(item => item === 'Audi').length > 0);  // true


// Approach #6:
console.log(!!['BMW', 'Toyota', 'Audi'].find(item => item === 'Audi'));  // true
console.log(Boolean(['BMW', 'Toyota', 'Audi'].find(item => item === 'Audi'))); // true


// Approach #7:
console.log(['BMW', 'Toyota', 'Audi'].findIndex(item => item === 'Audi') > -1); // true
What is event bubbling and event capturing?

Is JavaScript a pass-by-reference or pass-by-value language? and Why?

It is always passed by value in JavaScript. When we pass an object, the value of the variable is a reference. So, when we pass an object and change its property, those changes persist outside of the function. This makes it look like objects are passed by reference.

So in JavaScript, it feels like:

Primitives are passed by value, and objects are passed by reference.
function update(arr) {
    arr.push('Cathy');
}

const user = ['Jenny'];
update(user);
console.log(user);
// Output: ["Jenny", "Cathy"]
What are the main difference between a .forEach loop and a .map() loop?
Explain the prototypical inheritance with example

How to stop the setInterval function?

setInterval() method returns an interval ID, which we can pass to clearInterval() method to stop it.

const intervalId = setInterval(functionName, 5000);
clearInterval(intervalId);

How setTimeout(callback, 0) execute the callback synchronously or asynchronously?

What are the ways to empty a JavaScript array?

We can empty an array in multiple ways. Following are some ways to empty a JavaScript array.

let arrList = ['a', 'b', 'c']
// Approach #1
arrList.lenght = 0;

// Approach #2
arrList.splice(0, arrList.length);

// Approach #3
arrList = [];

// Approach #4
while(arrList.length > 0) {
    arrList.pop();
}

What will be the output of the following code and why?

function closureInLoop() {
  for(var i = 0; i < 3 ;i++) {
    setTimeout(function(){
      console.log(i)
    })
  }
}
closureInLoop();
   

What will be the output of the following code and why?

var popstar = 'Justin Bieber';
var obj = {
  popstar: 'Taylor Swift',
  getPopstarName: function() {
    return this.popstar;
  }
};
var temp = obj.getPopstarName;
console.log(temp());
   

How to stop adding, modifying, and deleting an object's properties?

By using Object.freeze() method:

const userObj = {
    name: 'Joe Smith',
    gender: 'Male',
    age: 40
}

Object.freeze(userObj);
userObj.age = 50;
console.log(userObj);
What are the differences between localStorage and cookie?
How can we stop the modification of an object in JavaScript?
How can we use constructor functions for inheritance in JavaScript?
How can we find the value of a variable in an array?
Why break statement inside forEach does not work?

What are the differences between hasAttribute vs hasOwnProperty?

hasAttribute():
hasAttribute() works only for html elements and returns true if that element has the same attribute name as the given argument.

<div id="container"></div>

<script>
    document.getElementById('container').hasAttribute('id'); //true
    document.getElementById('container').hasOwnProperty('id'); //false
</script>

hasOwnProperty():
hasOwnProperty() works only for JavaScript objects and returns true if that object has a property with the same name as the given argument.

    var obj = {
        name: "Joe Smith"
    };

    obj.hasOwnProperty("name"); //true
    obj.hasAttribute("name");  //false

Why do we need package-lock.json file?

Package-lock.json file stores an exact, versioned dependency tree rather than using starred versioning like package.json (e.g. 1.0.*). It guarantees the same dependencies for other developers or prod releases, etc.

The package.json contains only the direct dependencies, not the nested dependencies. In the standard package.json, we can not control the versions of nested dependencies, referencing them directly or as peer dependencies won't help as we can not control the version tolerance that the direct dependencies define for these nested dependencies.