Q: What is the difference between == and ===?
A: == checks for value equality and converts types if needed. === checks for both value and type.
Example: '5' == 5 // true, '5' === 5 // false
Q: Explain closures in JavaScript.
A: A closure is a function that remembers its outer variables even after the outer function has
finished.
Example:
function outer() {
let count = 0;
return function inner() {
count++;
return count;
};
Q: How does prototypal inheritance work?
A: Objects can inherit properties from other objects via their prototype.
Example:
let parent = { greet: function() { return 'hello'; } };
let child = Object.create(parent);
child.greet(); // 'hello'
Q: What is the event loop in JavaScript?
A: The event loop handles async operations. It places them in the queue and runs them when the
call stack is empty.
Q: Explain the concept of hoisting.
A: Hoisting means variable and function declarations are moved to the top of their scope before
code runs.
Example: console.log(a); var a = 5; // undefined
Q: What are the differences between var, let, and const?
A: var is function-scoped, let and const are block-scoped. const can't be reassigned.
Q: How does this keyword work in JavaScript?
A: 'this' refers to the object that is calling the function. Its value depends on how the function is
called.
Q: What is the purpose of the use strict directive?
A: It helps catch common coding mistakes and prevents unsafe actions.
Q: Explain the difference between synchronous and asynchronous code in JavaScript.
A: Synchronous code runs in order. Asynchronous code can run later, allowing other tasks to
continue meanwhile.
Q: What are Promises and how do they work?
A: Promises represent a value that may be available now or in the future.
Example:
new Promise((resolve) => resolve('done')).then(data => console.log(data));
Q: How does the async/await syntax work?
A: It allows writing asynchronous code in a synchronous style using 'await' to wait for Promises.
Q: What is the difference between null and undefined?
A: null is an assigned value. undefined means a variable hasn't been assigned a value yet.
Q: Explain the concept of callback functions.
A: A callback is a function passed as an argument to another function to be called later.
Q: What is the difference between forEach() and map()?
A: forEach() just iterates, map() returns a new array with results.
Example: [1,2,3].map(x => x * 2); // [2,4,6]
Q: How do you handle errors in JavaScript?
A: Use try...catch blocks or catch method in Promises.
Q: What is the purpose of the bind() method?
A: bind() sets the value of 'this' for a function and returns a new function.
Q: Explain the concept of event bubbling and capturing.
A: Bubbling: events go from child to parent. Capturing: from parent to child. You can choose phase
using addEventListener.
Q: What is memoization and how can it be implemented in JavaScript?
A: Memoization stores function results to avoid repeated calculations.
Example:
const memo = {}; function fib(n) {
if (n in memo) return memo[n];
if (n <= 1) return n;
return memo[n] = fib(n-1) + fib(n-2);
Q: How does JavaScript handle asynchronous programming?
A: Using callbacks, Promises, async/await, and the event loop.
Q: What are arrow functions and how do they differ from regular functions?
A: Arrow functions don't have their own 'this' and can't be used as constructors.
Q: Explain the concept of debouncing and throttling.
A: Debouncing: delays function call until pause in input. Throttling: ensures function is called at most
once in a time frame.
Q: What is the difference between Object.create() and the constructor pattern?
A: Object.create() creates a new object with a specified prototype. Constructor uses 'new' keyword
to set up prototype and properties.
Q: How does the module pattern work in JavaScript?
A: It encapsulates code using IIFE or modules to avoid polluting the global scope.
Q: What are generator functions and how are they used?
A: Generators can pause and resume with 'yield'.
Example:
function* gen() { yield 1; yield 2; }
Q: Explain the concept of currying in JavaScript.
A: Currying converts a function with multiple arguments into a series of functions taking one
argument each.
Q: How does the setTimeout() function work?
A: It schedules a function to run after a delay.
Example: setTimeout(() => console.log('Hi'), 1000);
Q: What is the purpose of the Array.prototype.reduce() method?
A: It reduces an array to a single value using a function.
Example: [1,2,3].reduce((a,b) => a + b, 0);
Q: How do you clone an object in JavaScript?
A: Use Object.assign({}, obj) or spread {...obj} or structuredClone(obj).
Q: What is the difference between call(), apply(), and bind()?
A: call and apply call the function immediately, bind returns a new function. apply uses array of
arguments.
Q: Explain the concept of lexical scope in JavaScript.
A: Lexical scope means a variable's scope is defined by its position in code when it's written.