JavaScript is one of the most in-demand programming languages, and mastering it is crucial for web development roles. Whether you are preparing for a junior, mid-level, or senior JavaScript interview, these commonly asked questions and answers will help you ace it.
1. What are the different data types in JavaScript?
Answer:
JavaScript has the following primitive data types:
String – Represents textual data (e.g.,
'Hello'
)Number – Represents numeric values (e.g.,
42
,3.14
)BigInt – Represents large integers (e.g.,
BigInt(9007199254740991)
)Boolean – Represents
true
orfalse
Undefined – Represents an uninitialized variable
Null – Represents an empty or unknown value
Symbol – Represents unique identifiers
Additionally, JavaScript has objects, which include arrays, functions, and other complex structures.
2. What is the difference between ==
and ===
in JavaScript?
Answer:
==
(loose equality) checks for value equality but allows type coercion.console.log(5 == "5"); // true
===
(strict equality) checks both value and type.console.log(5 === "5"); // false
Using ===
is recommended for better type safety.
3. What are closures in JavaScript?
Answer:
A closure is a function that remembers the variables from its outer scope even after the outer function has executed.
Example:
function outerFunction(outerVariable) {
return function innerFunction(innerVariable) {
console.log(`Outer: ${outerVariable}, Inner: ${innerVariable}`);
};
}
const newFunction = outerFunction("Hello");
newFunction("World"); // Output: Outer: Hello, Inner: World
Closures are useful for data encapsulation and maintaining state.
4. What is the difference between let
, const
, and var
?
Answer:
var
: Function-scoped, can be redeclared and updated.let
: Block-scoped, cannot be redeclared, but can be updated.const
: Block-scoped, cannot be redeclared or updated.
Example:
var x = 10;
let y = 20;
const z = 30;
Prefer let
and const
over var
to avoid scoping issues.
5. What is the event loop in JavaScript?
Answer:
The event loop is what enables JavaScript to be asynchronous. It continuously checks the call stack and the task queue, executing tasks in order.
Example:
console.log("Start");
setTimeout(() => console.log("Timeout"), 0);
console.log("End");
Output:
Start
End
Timeout
Even though setTimeout
is set to 0ms
, it executes last due to the event loop mechanism.
6. What is the difference between null
and undefined
?
Answer:
null
is an assigned value representing an intentional absence of value.undefined
means a variable has been declared but has not been assigned a value.
Example:
let a;
console.log(a); // undefined
let b = null;
console.log(b); // null
7. How does this
work in JavaScript?
Answer:
this
refers to the object that is currently executing the function.
In a regular function:
this
refers to the global object (window
in browsers,global
in Node.js).In a method:
this
refers to the object that owns the method.In an arrow function:
this
is lexically bound (inherits from the surrounding scope).
Example:
const obj = {
name: "John",
greet: function() {
console.log(`Hello, ${this.name}`);
}
};
obj.greet(); // Output: Hello, John
8. What are promises in JavaScript?
Answer:
Promises are used for handling asynchronous operations. A promise can be in one of three states:
Pending – Initial state
Fulfilled – The operation was successful
Rejected – The operation failed
Example:
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => resolve("Promise Resolved!"), 2000);
});
myPromise.then((message) => console.log(message));
9. What is the difference between map
, forEach
, and filter
?
Answer:
map()
– Returns a new array after applying a function to each element.forEach()
– Iterates over an array but does not return anything.filter()
– Returns a new array containing elements that pass a condition.
Example:
const nums = [1, 2, 3, 4, 5];
const doubled = nums.map(num => num * 2);
const evens = nums.filter(num => num % 2 === 0);
nums.forEach(num => console.log(num));
10. What is destructuring in JavaScript?
Answer:
Destructuring allows unpacking values from arrays or objects into separate variables.
Example:
const person = { name: "Alice", age: 25 };
const { name, age } = person;
console.log(name, age); // Alice 25
Conclusion
These JavaScript interview questions cover fundamental and advanced concepts that frequently appear in technical interviews. Practicing these will help you build confidence and improve your problem-solving skills. Keep coding and good luck with your interview!
Comments
Post a Comment