12 Most asked Javascript Interview Questions

sidtechtalks.in

Woman Reading

01/12

Q. Explain the various JavaScript datatypes?

JavaScript supports the following data types: 1. Boolean 2. Object 3. Undefined 4. Number 5. Symbol 6. Null 7. String

02/12

Q. What are the ways to define a variable in JS?

Var declares a variable and, optionally,  initializes its value. Ex: var a =10; Variable declarations are  processed before code execution. Const  functions are not permitted to modify the object to which they are  called. A const function can be called on any type of object. Let It indicates that the variable will only be used in the block in which it is defined.

03/12

Q. What is the DOM?

1. A Document Object Model is a programming interface for HTML and XML documents. 2. When the browser tries to render an HTML document, it creates an object based on the HTML document called DOM. 3. Using this, we can manipulate or change various elements inside the HTML document.

04/12

Q. Can you assign an anonymous function to a variable and pass it as an argument to another function?

Yes, we can assign an anonymous function to a variable. Moreover,  it can be passed as an argument to another function, which is also  called the Higher-Order Function.

05/12

Q. What is the purpose of the ‘this’ operator in JavaScript?

this’ keyword refers to the object that the function is a property of. Depending on where it is used, it has different values function() { console.log(this); } This above function is a property of global object. And the global object is window object.

06/12

Q. What is the purpose of the ‘this’ operator in JavaScript?

this’ keyword refers to the object that the function is a property of. Depending on where it is used, it has different values function() { console.log(this); } This above function is a property of global object. And the global object is window object.

07/12

Q. What is Callback?

A callback is a function that will be executed after another  function gets executed. The function that is used as an argument to  another function is called the Callback function.

08/12

Q. What is the difference between Rest & Spread operators?

1. Rest parameter should always be used as the  last parameter of the function. It is used to take several arguments and  turn them into an array. Whereas the Spread operator takes an array or an object and spreads it. 2. Rest is used in a function declaration, whereas the Spread operator is used in function calls.

09/12

Q. What is Object Destructuring? How it is useful?

It is a new way to extract elements from an object or an array.  You don’t need to initialize the individual variables. It is easy to  use, understand, and readable. For example: var {var1, var2, var3} = {'one', 'two', 'three'}

10/12

Q. Explain difference between event.preventDefault() and event.stopPropagation() ?

The event.preventDefault() method prevents the default behavior of an element. If used in a form element it prevents it from submitting. While the event.stopPropagation() method stops the propagation of an event. It stops the event from occurring in the bubbling or capturing  phase. It does not prevent any default behaviors from  occurring; for instance, clicks on links are still processed.

11/12

Q. What is call() and apply() functions?

When the number of the function’s arguments are known to the  programmer, as they have to be mentioned as arguments in the call  statement. Similar to call() but the only difference is we don’t know  the number of parameters in the function. So, we can pass the array  like ([2, 3]) instead of (2, 3).

12/12

Q. What are Promises? How many types of states?

It is used to handle asynchronous operations in javascript. Before  promises, callbacks were used to handle asynchronous operation. But due  to limited functionality of callback, using multiple callbacks to  handle async code can lead to unmanageable code. It has four states:- 1. Pending 2. Fulfilled 3. Rejected  4. Settled