10 Javascript interview Questions and Answers

Md.Saiful Islam
3 min readMay 12, 2020

Here is 10 Javascript interview Questions and Answers —

  1. What are the different ways to get an element from DOM?

Answer:

we can use the following methods in document

  • getElementById to get a element that has the provided Id.
  • getElementsByClassName to get a nodelist by providing a class name.
  • getElementsByTagName to get a nodelist by the provided tag name.
  • querySelector you will pass css style selector and this will return first matched element in the DOM.
  • querySelectorAll will return a non-live nodelist by using depth-first pre order traversal of all the matched elements. Non-live means, any changes after selecting the elements will not be reflected.

2. What’s the difference between undefined and null?

Answer:

  • undefined is the default value of a variable that has not been assigned a specific value Or a function that has no explicit return value ex. console.log(1). Or a property that does not exist in an object.
  • null is “a value that represents no value”. null is value that has been explicitly defined to a variable.

3. What’s Event Bubbling?

Answer:

When an event occurs on a DOM element, that event does not entirely occur on that just one element. In the Bubbling Phase, the event bubbles up or it goes to its parent, to its grandparents, to its grandparent’s parent until it reaches all the way to the window.

4. What’s the difference between event.preventDefault() and event.stopPropagation() ?

Answer:

The event.preventDefault() method prevents the default behavior of an element. While the event.stopPropagation() method stops the propogation of an event or it stops the event from occurring in the bubbling or capturing phase.

5.What’s the difference between Function.prototype.apply and Function.prototype.call?

Answer:

In apply we pass the arguments as an array and in call we pass the arguments directly in the argument list.

6.Implement the Array.prototype.map method by hand.

Answer:

function map(arr, mapCallback) {if (!Array.isArray(arr) || !arr.length || typeof mapCallback !== 'function') {return [];} else {let result = [];for (let i = 0, len = arr.length; i < len; i++) {result.push(mapCallback(arr[i], i, arr));}return result; // return the result array}}

7.Implement the Array.prototype.filter method by hand.

Answer:

function filter(arr, filterCallback) {// First, we check if the parameters passed are right.if (!Array.isArray(arr) || !arr.length || typeof filterCallback !== 'function'){return [];} else {let result = [];// We're making a results array every time we call this function// because we don't want to mutate the original array.for (let i = 0, len = arr.length; i < len; i++) {// check if the return value of the filterCallback is true or "truthy"if (filterCallback(arr[i], i, arr)) {// push the current item in the 'result' array if the condition is trueresult.push(arr[i]);}}return result; // return the result array}}

8.What is async/await and How does it work?

Answer:

async/await is the new way of writing asynchronous or non-blocking code in JavaScript’s. It is built on top of Promises. It makes writing asynchronous code more readable and cleaner than Promises and Callbacks.

9.What’s the difference between Spread operator and Rest operator?

Answer:

Spread operators extract an array to an individual variable while rest operator make an array by using distinct variable.

10.What are the ways to deal with Asynchronous Code in JavasScript?

Answer:

· Callbacks

· Promises

· async/await

· Libraries like async.js, bluebird, q, co.

That’s all for today.

--

--