Top 10 JavaScript Interview Question

1 . What are the differences between ==
and ===
?
Answer : The difference between ==
(abstract equality) and ===
(strict equality) is that the ==
compares by value after coercion and ===
compares by value and type without coercion.
Rules of implicit coercion :
- If one operands are string and another is number , string will converted to number .
- if one operand is Boolean and others is non-boolean , Boolean will converted to number.
- While comparing string or number with an object , the object will be converted to primitive type .
2. Explain Event Bubbling ?
Answer : When an event is triggered from an element, the event handler / event listener tied to that event is called. When an event is fired on an element that has parent elements, it runs through a “bubbling” phase. During the “bubbling” phase, the browser checks to see if the element that triggered the event has an event handler registered to it. If it does, it runs the event handler. If it does not, it moves to the parent element and checks if it has an event handler registered to it. The browser continues to move up the chain of parent elements, checking for and executing registered event handlers, until it reaches the root element.
3 . What’s the value of this
in JavaScript?
Answer: Basically, this
refers to the value of the object that is currently executing or invoking the function. I say currently due to the reason that the value of this changes depending on the context on which we use it and where we use it.
4 . What is an IIFE, what is the use of it?
Answer : An IIFE or Immediately Invoked Function Expression is a function that is gonna get invoked or executed after its creation or declaration.
5 . How can you create a method on instance of a date which will give you next day?
Answer: I have to declare a method on the prototype of Date object. To get access to the current value of the instance of the date, i will use this
Date.prototype.nextDate = function() {
const currentDate = this.getDate();
return new Date(this.setDate(currentDate + 1))
}const date = new Date();console.log(date.getDate());
// 12
console.log(date.nextDate());
//Wed May 13 2020 20:25:38 GMT+0600 (Bangladesh Standard Time)
6 . What’s the difference between event.preventDefault()
and event.stopPropagation()
methods?
Answer : 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 or it stops the event from occurring in the bubbling or capturing phase.
7 . What is the usage of Function.prototype.bind
?
Answer: The bind
method returns a new function that is bound
to a specific this
value or the "owner" object, So we can use it later in our code. The call
,apply
methods invokes the function immediately instead of returning a new function like the bind
method.
8 . What’s the difference between Function.prototype.apply
and Function.prototype.call ?
Answer : The only difference between apply
and call
is how we pass the arguments in the function being called. In apply
we pass the arguments as an array and in call
we pass the arguments directly in the argument list by comma separating.
9 . Explain Functional Programming ?
Answer: Functional Programming is a declarative programming paradigm or pattern on how we build our applications with functions using expressions that calculates a value without mutating or changing the arguments that are passed to it.
10 . What is Callback function?
Answer : A callback function is a function that is passed as an argument to another function, to be “called back” at a later time.