π Ultimate Guide: JavaScript Interview Questions and Answers (2025 Edition) - Part 1
JavaScript is a must-know language for front-end and back-end developers. If you're preparing for JavaScript interviews, this guide covers commonly asked questions with detailed answers to help you succeed. π
Letβs dive in! π₯
π’ Basic JavaScript Interview Questions
1. What is JavaScript?
JavaScript is a high-level, interpreted programming language used to create interactive web pages. It supports event-driven, functional, and object-oriented programming paradigms.
2. What are the different data types in JavaScript?
JavaScript has primitive and non-primitive data types:
-
Primitive:
String
,Number
,Boolean
,Null
,Undefined
,Symbol
,BigInt
-
Non-primitive:
Object
,Array
,Function
3. What is the difference between let
, const
, and var
?
Keyword | Scope | Reassignable? | Hoisted? |
---|---|---|---|
var |
Function-scoped | β Yes | β Yes (but undefined) |
let |
Block-scoped | β Yes | β No |
const |
Block-scoped | β No | β No |
4. What are template literals?
Template literals allow embedding expressions inside strings using backticks.
const name = "Atul";
console.log(`Hello, ${name}!`); // Hello, Atul!
5. What is the difference between ==
and ===
?
-
==
(Abstract Equality): Converts operands to the same type before comparing. -
===
(Strict Equality): Compares both value and type.
console.log(5 == "5"); // true (type conversion)
console.log(5 === "5"); // false (different types)
π‘ Intermediate JavaScript Interview Questions
6. Explain closures in JavaScript.
A closure is a function that retains access to its outer functionβs scope even after the outer function has finished executing.
function outer() {
let count = 0;
return function inner() {
count++;
console.log(count);
};
}
const counter = outer();
counter(); // 1
counter(); // 2
7. What is the difference between synchronous and asynchronous programming?
- Synchronous: Code runs sequentially, blocking execution.
- Asynchronous: Code runs in the background (e.g., callbacks, promises, async/await).
8. What is the this
keyword in JavaScript?
- Refers to the object that is executing the function.
- Its value depends on where it is used:
const obj = {
name: "Atul",
greet() {
console.log(this.name);
},
};
obj.greet(); // "Atul"
9. What is event delegation in JavaScript?
Event delegation is a technique where a parent element handles events for its child elements, improving performance.
document.querySelector("ul").addEventListener("click", (e) => {
if (e.target.tagName === "LI") {
console.log(e.target.textContent);
}
});
10. What are promises in JavaScript?
A Promise represents a value that might be available now, later, or never.
const fetchData = new Promise((resolve, reject) => {
setTimeout(() => resolve("Data received"), 2000);
});
fetchData.then(console.log); // "Data received" after 2 seconds
π΄ Advanced JavaScript Interview Questions
11. What is the difference between call
, apply
, and bind
?
Method | Usage |
---|---|
call() |
Calls a function with a specific this value and arguments passed individually. |
apply() |
Calls a function with a specific this value and arguments passed as an array. |
bind() |
Returns a new function with a bound this value. |
Example:
const obj = { name: "Atul" };
function greet(greeting) {
console.log(`${greeting}, ${this.name}`);
}
greet.call(obj, "Hello"); // "Hello, Atul"
greet.apply(obj, ["Hi"]); // "Hi, Atul"
const boundFunc = greet.bind(obj);
boundFunc("Hey"); // "Hey, Atul"
12. Explain the event loop in JavaScript.
The event loop allows JavaScript to handle asynchronous operations by placing tasks into different queues and executing them when the call stack is empty.
13. What are setTimeout
and setInterval
?
-
setTimeout()
: Executes code after a delay. -
setInterval()
: Repeats execution at a fixed interval.
setTimeout(() => console.log("Hello after 2s"), 2000);
const interval = setInterval(() => console.log("Repeating every 1s"), 1000);
clearInterval(interval); // Stops interval execution
14. What is debouncing and throttling in JavaScript?
- Debouncing: Limits function execution until a pause occurs.
- Throttling: Ensures a function executes at most once in a given period.
Example (Debounce):
function debounce(func, delay) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func(...args), delay);
};
}
15. What is the difference between shallow and deep copy?
- Shallow Copy: Copies only references for nested objects.
- Deep Copy: Creates a completely independent copy.
Example:
let obj1 = { a: 1, b: { c: 2 } };
let shallowCopy = { ...obj1 };
let deepCopy = JSON.parse(JSON.stringify(obj1));
π― Final Thoughts
Mastering these JavaScript interview questions will help you crack front-end and full-stack developer interviews! Keep practicing and stay updated with the latest JS features. π
π¬ Got more questions? Drop them in the comments! π
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.