javascript interview questions and answers 2022
Difference between let and const in JavaScript – Best practices and example #javascript interview questions and answers 2025
Preparing for a JavaScript interview in 2025 requires a solid understanding of both fundamental and advanced concepts. Below is a curated list of common JavaScript interview questions, along with concise answers to help you prepare effectively.
JavaScript has seven primitive data types:
"Hello"
.42
or 3.14
.true
or false
.null
, undefined
, and undeclared variables?let a;
console.log(a); // Output: undefined
let b = null;
console.log(b); // Output: null
console.log(c); // ReferenceError: c is not defined
while
and do-while
loops in JavaScript?let i = 0;
while (i < 5) {
console.log(i);
i++;
}
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
for
loop:
const arr = [1, 2, 3];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
forEach
method:
arr.forEach(item => console.log(item));
for...of
loop:
for (const item of arr) {
console.log(item);
}
for...in
loop:
const obj = { a: 1, b: 2 };
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(key, obj[key]);
}
}
Object.keys
with forEach
:
Object.keys(obj).forEach(key => {
console.log(key, obj[key]);
});
Promises are objects representing the eventual completion or failure of an asynchronous operation. They have three states:
const promise = new Promise((resolve, reject) => {
// asynchronous operation
if (success) {
resolve(result);
} else {
reject(error);
}
});
promise
.then(result => {
// handle success
})
.catch(error => {
// handle error
});
An Immediately Invoked Function Expression (IIFE) is a function that runs as soon as it's defined. It's commonly used to create a local scope and avoid polluting the global namespace.
(function() {
// code inside IIFE
})();
Event delegation is a technique where a single event listener is added to a parent element to manage events for its child elements. This is efficient for handling events on multiple child elements, especially when they are dynamically added or removed.
<ul id="parent">
<li>Item 1</li>
<li>Item 2</li>
</ul>
<script>
document.getElementById('parent').addEventListener('click', function(event) {
if (event.target.tagName === 'LI') {
console.log('Clicked on', event.target.textContent);
}
});
</script>
this
works in JavaScript.
The value of this
depends on the context in which a function is called:
this
refers to the global object (window
in browsers).this
refers to the global object in non-strict mode and undefined
in strict mode.this
refers to that object.new
, this
refers to the newly created object.const obj = {
name: 'JavaScript',
showName: function() {
console.log(this.name);
}
};
obj.showName(); // Output: JavaScript
SEO-Optimized Article: Understanding JavaScript Closures & ES6 Features
A closure is a function that retains access to its lexical scope, even when executed outside that scope. Closures are widely used in JavaScript for data encapsulation, callbacks, and maintaining state.
function outer() {
let name = "Developer Indian";
function inner() {
console.log(name);
}
return inner;
}
let closure = outer();
closure(); // Output: Developer Indian
let
, const
, and var
Keyword | Scope | Reassignment Allowed | Hoisting |
---|---|---|---|
var |
Function-scoped | Yes |
Hoisted with undefined |
let |
Block-scoped | Yes | Hoisted but not initialized |
const |
Block-scoped | No | Hoisted but not initialized |
Arrow functions provide a shorter syntax and do not have their own this
context, making them useful in callbacks.
let add = (a, b) => a + b;
console.log(add(2, 3)); // Output: 5
this
(inherits from parent scope)Destructuring simplifies extracting values from arrays or objects.
let [a, b] = [1, 2];
console.log(a); // Output: 1
const user = { name: "John", age: 30 };
const { name, age } = user;
console.log(name); // Output: John
Event delegation allows you to attach a single event listener to a parent element, handling events from child elements efficiently.
document.querySelector("ul").addEventListener("click", function(event) {
if (event.target.tagName === "LI") {
console.log("List item clicked!");
}
});
Propagation Type | Description |
---|---|
Event Bubbling | Events propagate up from target to root. |
Event Capturing | Events propagate down from root to target. |
console.log()
(Basic debugging tool)debugger
statement (Pauses execution for inspection)Strict mode enforces stricter parsing and error handling.
"use strict";
x = 10; // Throws an error
JavaScript is a versatile and powerful language, and mastering its concepts is crucial for any developer. This guide covers top JavaScript interview questions and answers for 2025, helping you prepare for your next interview. From core concepts like inheritance and closures to advanced topics like promises and ES6 features, you’re now equipped to tackle any JS interview with confidence.
These are just some of the most commonly asked JavaScript interview questions. Preparing for a JavaScript interview requires practice, hands-on coding, and a deep understanding of the language's core concepts. Good luck!