JavaScript Interview Questions
Here are accurate and concise answers to your JavaScript interview questions:
31. What is JavaScript?
JavaScript is a lightweight, interpreted programming language used for creating interactive web applications. It enables dynamic content, form validation, animations, and API interactions.
32. Explain the difference between let
, const
, and var
in JavaScript.
var
: Function-scoped, can be redeclared, and hoisted withundefined
.let
: Block-scoped, cannot be redeclared in the same scope, but can be reassigned.const
: Block-scoped, cannot be reassigned or redeclared.
33. Describe hoisting in JavaScript.
Hoisting is JavaScript’s default behavior of moving variable and function declarations to the top of their scope before execution.
Example:
console.log(x); // Undefined
var x = 10;
34. What is the purpose of the this
keyword in JavaScript?
The this
keyword refers to the object that is executing the function. Its value depends on how the function is called.
35. What are closures in JavaScript?
A closure is a function that remembers and accesses variables from its outer scope even after the outer function has executed.
Example:
function outer() {
let count = 0;
return function inner() {
count++;
console.log(count);
};
}
const counter = outer();
counter(); // 1
counter(); // 2
36. Explain the concept of prototypal inheritance in JavaScript.
Objects in JavaScript can inherit properties and methods from other objects using prototypes. Each object has an internal [[Prototype]]
that refers to another object.
37. How does event delegation work in JavaScript?
Event delegation allows us to handle events efficiently by attaching a single event listener to a parent element instead of multiple child elements.
Example:
document.getElementById("parent").addEventListener("click", function (e) {
if (e.target.matches(".child")) {
console.log("Child clicked");
}
});
38. Describe the difference between ==
and ===
in JavaScript.
==
(loose equality) compares values after type conversion.===
(strict equality) compares both value and type.
39. What is the purpose of the async
keyword in JavaScript?
The async
keyword makes a function return a Promise and allows await
to be used inside it for handling asynchronous operations.
40. How do you handle errors in JavaScript?
Using try...catch
:
try {
let result = riskyOperation();
} catch (error) {
console.log("Error occurred:", error);
}
41. Explain the concept of callback functions.
A callback is a function passed as an argument to another function and executed later.
Example:
function greet(name, callback) {
console.log("Hello, " + name);
callback();
}
greet("Byomkesh", function () {
console.log("Callback executed");
});
42. What is the difference between null
and undefined
in JavaScript?
null
: Represents an intentional absence of a value.undefined
: Means a variable has been declared but not assigned a value.
43. Describe the role of the bind
method in JavaScript.
The bind
method creates a new function with this
bound to a specific object.
Example:
let person = { name: "Byomkesh" };
function greet() {
console.log(this.name);
}
let boundGreet = greet.bind(person);
boundGreet(); // "Byomkesh"
44. What is the purpose of the map
function in JavaScript?
The map
method creates a new array by applying a function to each element of an existing array.
Example:
const numbers = [1, 2, 3];
const doubled = numbers.map(n => n * 2); // [2, 4, 6]
45. Explain the concept of promises in JavaScript.
A Promise is an object representing the eventual completion or failure of an asynchronous operation.
Example:
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("Success"), 1000);
});
promise.then(console.log); // "Success"
46. What is the event loop in JavaScript?
The event loop is a mechanism that handles asynchronous operations by pushing callback functions to the call stack when the main thread is free.
47. Describe the difference between null
, undefined
, and undeclared in JavaScript.
null
: Explicitly assigned to indicate no value.undefined
: A variable declared but not assigned a value.- Undeclared: A variable that has never been declared.
48. How do you create an object in JavaScript?
Using object literals:
let person = { name: "Byomkesh", age: 25 };
Using Object.create()
:
let obj = Object.create(null);
Using constructor functions:
function Person(name) {
this.name = name;
}
let p = new Person("Byomkesh");
49. Explain the purpose of the localStorage
and sessionStorage
objects.
localStorage
: Stores data with no expiration.sessionStorage
: Stores data only for the duration of the session.
50. How does the typeof
operator work in JavaScript?
The typeof
operator returns the type of a variable.
Example:
console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof {}); // "object"
Let me know if you need further explanations! 🚀