Top 10 JavaScript Interview Questions
let
, var
, and const
in JavaScript?null
and undefined
in JavaScript?==
and ===
in JavaScript?this
keyword in JavaScript?setTimeout
and setInterval
?
1. What is the difference between let
, var
, and const
in JavaScript?
Answer:
var
: Declares a variable with function-scoped or globally scoped if declared outside a function. Variables declared withvar
can be re-declared and updated.let
: Declares a block-scoped variable that can be updated but not re-declared within the same scope.const
: Declares a block-scoped constant. Its value cannot be changed after initialization and cannot be re-declared.
Example:
2. What is a closure in JavaScript?
Answer:
A closure is a function that "remembers" its lexical environment, even after the function that created it has finished executing. This allows the closure to access variables from its outer function even after the outer function has returned.
Example:
3. What are promises in JavaScript?
Answer:
A promise is an object representing the eventual completion or failure of an asynchronous operation. It allows chaining with .then()
for success and .catch()
for errors.
Example:
4. What is the difference between null
and undefined
in JavaScript?
Answer:
null
: Represents an intentionally empty or non-existent value. It's an object.undefined
: Represents a variable that has been declared but has not been assigned a value.
Example:
5. What is event delegation in JavaScript?
Answer:
Event delegation is a technique in JavaScript where you attach a single event listener to a parent element, instead of attaching event listeners to individual child elements. This is efficient as it leverages event bubbling to handle events.
Example:
6. What is the difference between ==
and ===
in JavaScript?
Answer:
==
(Equality operator): Compares values for equality but performs type coercion if the values are of different types.===
(Strict equality operator): Compares both value and type without type coercion.
Example:
7. Explain hoisting in JavaScript.
Answer:
Hoisting is JavaScript's default behavior of moving declarations to the top of their containing scope (function or global scope). Variables declared with var
and functions are hoisted, but only the declaration, not the initialization.
Example:
8. What is the this
keyword in JavaScript?
Answer:
The this
keyword refers to the current context or object the function is called on. The value of this
depends on how the function is called:
- Inside a regular function:
this
refers to the global object (in browser, it'swindow
). - Inside an object method:
this
refers to the object. - Inside a constructor:
this
refers to the newly created object. - Arrow functions:
this
is lexically bound to the context in which the function is defined.
Example:
9. What is the difference between setTimeout
and setInterval
?
Answer:
setTimeout()
: Executes a function once after a specified delay.setInterval()
: Repeatedly executes a function with a specified time delay between each call.
Example:
10. What is the spread operator in JavaScript?
Answer:
The spread operator (...
) allows you to spread the elements of an array or object into a new array or object.
Example:
These are some of the most common JavaScript interview questions that are frequently asked, along with detailed explanations and examples. Understanding these concepts will help you prepare well for your interview.
Comments
Post a Comment