OOPS JavaScript Interview Questions PDF By ScholarHat
scholarhateducation
70 views
29 slides
Mar 16, 2025
Slide 1 of 29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
About This Presentation
OOPS JavaScript Interview Questions PDF By ScholarHat
Size: 2.49 MB
Language: en
Added: Mar 16, 2025
Slides: 29 pages
Slide Content
Top OOPS JavaScript Interview Questions You
Should Know!
OOPS JavaScript Interview Questions
Understanding OOPS (Object-Oriented Programming) principles in JavaScript is important for developers
preparing for technical interviews. You might face questions about key ideas like classes, objects, Different
Types of Inheritance, encapsulation, and polymorphism. You’ll also need to explain how JavaScript uses
these concepts, which can be different because of its prototype-based inheritance.
What to Expect in OOPS JavaScript Interview Questions
OOPS JavaScript interview questions assess your understanding of Object-Oriented Programming principles
and their application in JavaScript. You may face questions about key concepts like classes, objects,
inheritance, encapsulation, and polymorphism and how they are implemented in JavaScript's unique
prototype-based structure.
Read More: Object-Oriented Programming Concepts
This Interview tutorial covers common OOPS questions in JavaScript interviews, with simple explanations
and examples to help you learn. By the end, you'll feel ready to answer questions confidently and use OOPS
principles to write clear and organized JavaScript code.
Ans:
Interviewers may ask you to explain real-world use cases, compare OOPS principles, or write code to
demonstrate these concepts in action. Being prepared for both fundamental and advanced scenarios will help
you excel, regardless of your experience level.
OOPS JavaScript Interview Questions for Freshers
1. What is Object-Oriented Programming (OOP)?
Ans: Object-Oriented Programming (OOP) is a programming paradigm that organizes data and behavior into
reusable structures called objects. It focuses on four main principles: encapsulation, inheritance,
polymorphism, and abstraction. OOP helps make code more modular, maintainable, and scalable.
2. What is a class in JavaScript?
Try it Yourself >>
A class in JavaScript is a blueprint for creating objects with shared properties and methods. It is defined
using the class keyword. For example:
Ans: An object in JavaScript is a standalone entity that contains properties (key-value pairs) and methods. It
is like a real-world entity with attributes and behaviors.
No direct output. Run the code to see results.
class Person {
constructor(name, age) {
}
greet() {
this.name = name;
this.age = age;
}
}
console.log(`Hello, my name is ${this.name}`);
Output
3. What is an object in JavaScript?
Output
Toyota
let car = {
brand: "Toyota",
model: "Corolla",
drive: function() {
}
};
console.log(car.brand); // Toyota
console.log("The car is driving");
4. What is encapsulation in OOP?
Ans:
Try it Yourself >>
Encapsulation is the concept of wrapping data (properties) and behavior (methods) into a single unit (class).
Ans:
Ans:
It restricts direct access to some components, providing controlled access through methods.
Inheritance allows one class (child class) to acquire the properties and methods of another class (parent
class). It is implemented using the extends keyword.
5. Explain inheritance in JavaScript.
6. What is polymorphism in JavaScript?
Read More: Difference Between Method Overloading and Method Overriding in Java
Polymorphism means "many forms." In JavaScript, it allows different objects to respond uniquely to the same
method call, often using method overriding or function overloading.
Ans: Abstraction involves hiding the complex implementation details of functionality and showing only the
necessary features. In JavaScript, this can be achieved through classes and objects.
7. What is an abstraction in JavaScript?
8. What is the difference between class and object?
Try it Yourself >>
Ans: You create a class in JavaScript using the keyword. For example:
Ans: Getters and setters are special methods in JavaScript that allow you to define custom logic when
accessing or updating a property.
Ans: A class is a blueprint for creating objects. An object is an instance of a class containing actual values for
the properties and methods defined in the class.
9. How do you create a class in JavaScript?
10. What are getters and setters in JavaScript?
class
class Circle {
constructor(radius) {
}
get radius() {
this._radius = radius;
}
set radius(value) {
return this._radius;
if (value > 0) {
this._radius = value;
} else {
console.log("Radius must be positive");
}
No direct output. Run the code to see results.
class Animal {
constructor(name) {
}
speak() {
this.name = name;
}
}
console.log(`${this.name} makes a noise`);
Output
Output
Output
}
}
Hello, John
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log("Hello, " + this.name);
};
let john = new Person("John");
john.greet(); // Hello, John
class Person {
constructor(name) {
}
greet() {
this.name = name;
console.log("Hi, I'm " + this.name);
No direct output. Run the code to see results.
11. What is the prototype in JavaScript?
Ans: In JavaScript, every object has a built-in property called Prototype in JavaScript. It allows you to add
methods and properties to objects, enabling inheritance.
12. What is a constructor in JavaScript?
Ans: A constructor is a special method that initializes an object when a class is called.
Try it Yourself >>
Try it Yourself >>
Output
Output
Dogs bark
Hi, I'm Alice
}
}
let alice = new Person("Alice");
alice.greet(); // Hi, I'm Alice
class Animal {
sound() {
}
}
class Dog extends Animal {
sound() {
console.log("Animals make sound");
}
}
let dog = new Dog();
dog.sound(); // Dogs bark
console.log("Dogs bark");
Try it Yourself >>
Try it Yourself >>
Read More: Java Constructor
Ans: Method overriding occurs when a subclass provides a specific implementation of a method already
defined in its parent class.
13. What is method overriding in JavaScript?
14. How do you create an object in JavaScript?
Ans: The
Try it Yourself >>
Try it Yourself >>
Read More: Static Keyword in Java
Ans: You can create an object using curly braces , the method, or by using a class.
keyword refers to the object it belongs to. Its value depends on how the function is called.
Ans: Static methods belong to the class and can be called without creating an object. Instance methods
belong to objects and require an object to be called.
8
Tesla
this
let user = {
name: "Sam",
greet: function() {
class MathUtils {
static add(a, b) {
}
}
console.log(MathUtils.add(3, 5)); // 8
return a + b;
{}
let car = { brand: "Tesla", color: "Blue" };
console.log(car.brand); // Tesla
new Object()
Output
Output
16. What is a "this" keyword in JavaScript?
15. What is the difference between static and instance methods?
Output
Output
5
Hi, Sam
}
};
user.greet(); // Hi, Sam
console.log("Hi, " + this.name);
class Circle {
#radius;
constructor(radius) {
}
getRadius() {
this.#radius = radius;
}
}
let circle = new Circle(5);
console.log(circle.getRadius()); // 5
return this.#radius;
#
Try it Yourself >>
Try it Yourself >>
Read More: This Keyword in Java
Read More: Java Access Modifiers: Default, Private, Protected, Public
Ans: Access modifiers control access to class properties or methods. JavaScript uses
proper ties.
for private
17. What are access modifiers in JavaScript?
18. How does inheritance work in JavaScript?
Ans: Inheritance allows one class to use properties and methods of another class using the
keyword.
19. What is the difference between "==" and "==="?
Ans: The
equality.
Try it Yourself >>
Try it Yourself >>
operator checks value equality, ignoring type. The operator checks both value and type
true
false
==
Car is starting
console.log(5 == "5"); // true
console.log(5 === "5"); // false
class Vehicle {
start() {
}
}
class Car extends Vehicle {
start() {
console.log("Vehicle is starting");
}
}
let car = new Car();
car.start(); // Car is starting
console.log("Car is starting");
===
extends
Output
Output
20. What is the purpose of the method?
21. What is the difference between function declaration and function
expression in JavaScript?
Ans: A function declaration is hoisted, so you can call it before it's defined. A function expression is not
hoisted, so you can only call it after it's defined.
Object.create()
Ans: The
Try it Yourself >>
method creates a new object using an existing object as its prototype.
Hello!
Object.create()
// Function Declaration
greet();
function greet() {
console.log("Hello!");
let person = {
greet: function() {
}
};
let friend = Object.create(person);
friend.greet(); // Hello!
console.log("Hello!");
Output
OOPS JavaScript Interview Questions for Intermediate
HTML to PDF
Output
Output
}
Hello!
Hi!
Hello, Raj
function greet(name) {
return function () {
console.log("Hello, " + name);
};
}
let greetRaj = greet("Raj");
greetRaj(); // Hello, Raj
this
// Function Expression
// greetMe(); // Error: Cannot access before initialization
const greetMe = function () {
console.log("Hi!");
};
greetMe();
22. What are closures in JavaScript?
Ans: Closures allow a function to remember and access its scope even when the function is executed outside
that scope.
23. What is the difference between call, apply, and bind methods in
JavaScript?
Ans: These methods are used to control the value of
Try it Yourself >>
Try it Yourself >>
in a function:
call()
apply()
bind()
Hello, Priya!
Hi, Priya!
Hey, Priya?
class
let user = {
name: "Priya",
};
function greet(greeting, punctuation) {
console.log(greeting + ", " + this.name + punctuation);
}
greet.call(user, "Hello", "!"); // Hello, Priya!
greet.apply(user, ["Hi", "!"]); // Hi, Priya!
let boundGreet = greet.bind(user, "Hey");
boundGreet("?"); // Hey, Priya?
class Student {
constructor(name, grade) {
}
}
let ramesh = new Student("Ramesh", 10);
ramesh.displayInfo(); // Ramesh is in grade 10
console.log(this.name + " is in grade " + this.grade);
Try it Yourself >>
Ans: You can create a class using the
: Calls a function with arguments passed individually.
: Calls a function with arguments passed as an array.
: Returns a new function with a specific this value.
keyword. Classes can have a constructor and methods.
Output
24. How do you create a class in JavaScript?
Output
Output
Output
Data received
[1, 2, 3, 4, 5]
Ramesh is in grade 10
...
let numbers = [1, 2, 3];
let newNumbers = [...numbers, 4, 5];
console.log(newNumbers); // [1, 2, 3, 4, 5]
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("Data received"), 2000);
});
promise.then((data) => console.log(data)); // Data received (after 2 seconds)
26. What are promises in JavaScript?
Ans: Promises are used to handle asynchronous operations. A promise can be in one of three states:
pending, fulfilled, or rejected.
27. What is destructuring in JavaScript?
25. What is the use of the spread operator?
Ans: The spread operator (
Try it Yourself >>
Try it Yourself >>
Try it Yourself >>
) allows you to expand arrays or objects into individual elements or properties.
Ans: The
resolves.
Try it Yourself >>
Try it Yourself >>
keyword makes a function return a promise and
Ans: Destructuring allows you to extract values from arrays or objects into variables.
Ans: Arrow functions are a shorter syntax for writing functions. They do not have their own
pauses execution until the promise
Output
Output
Anita
25
Data loaded
async
let add = (a, b) => a + b;
console.log(add(5, 3)); // 8
let person = { name: "Anita", age: 25 };
let { name, age } = person;
console.log(name); // Anita
console.log(age); // 25
async function fetchData() {
let data = await Promise.resolve("Data loaded");
console.log(data);
}
fetchData(); // Data loaded
await
this.
28. How does the
29. What are arrow functions in JavaScript?
syntax work in JavaScript?
async/await
Output
Output
8
Something went wrong
Cleanup done
try catch
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
console.log("Hello, " + this.name);
};
try {
throw new Error("Something went wrong");
} catch (error) {
console.log(error.message);
} finally {
console.log("Cleanup done");
}
finally
31. What is the prototype in JavaScript?
Ans: A prototype is an object from which other objects inherit properties. Every JavaScript object has a
prototype that links to other objects for property sharing.
30. How can you handle errors in JavaScript?
Ans: You can handle errors using
Try it Yourself >>
Try it Yourself >>
,
Read More: Exception Handling in Java
, and blocks.
Output
Output
Hello, Neeraj
Object.create()
Button clicked: Button Text
let neeraj = new Person("Neeraj");
neeraj.greet(); // Hello, Neeraj
let parent = {
greet: function () {
console.log("Hello from parent");
},
};
let child = Object.create(parent);
child.greet(); // Hello from parent
document.getElementById("parent").addEventListener("click", function (event) {
if (event.target.tagName === "BUTTON") {
}
});
console.log("Button clicked: " + event.target.textContent);
33. How does
32. What is event delegation in JavaScript?
Ans: Event delegation is a technique where you attach a single event listener to a parent element to manage
events for its child elements.
work in JavaScript?
Ans: The
Try it Yourself >>
Try it Yourself >>
method creates a new object with the specified prototype object.
Object.create()
Output
Output
Start
End
Async Task
Hello from parent
// Synchronous code
console.log("Start");
console.log("End");
// File: math.js
export function add(a, b) {
return a + b;
}
// Asynchronous code
console.log("Start");
setTimeout(() => console.log("Async Task"), 1000);
console.log("End");
35. What is a module in JavaScript?
Ans: A module is a reusable piece of JavaScript code that can be imported or exported between files using
import and export.
34. What is the difference between synchronous and asynchronous code?
Ans: Synchronous code executes line by line, blocking further execution until the current task is completed.
Asynchronous code runs tasks in the background, allowing other operations to continue.
Try it Yourself >>
Try it Yourself >>
Output
Output
8
Aman
30
// File: main.js
import { add } from "./math.js";
console.log(add(3, 5)); // 8
// Shallow Copy
let obj1 = { name: "Aman" };
let shallowCopy = Object.assign({}, obj1);
shallowCopy.name = "Ravi";
console.log(obj1.name); // Aman
// Deep Copy
let obj2 = { age: 30 };
let deepCopy = JSON.parse(JSON.stringify(obj2));
deepCopy.age = 25;
console.log(obj2.age); // 30
OOPS JavaScript Interview Questions for Experienced
36. What is the difference between deep copy and shallow copy?
Ans: A shallow copy copies the reference of objects, so changes in one affect the other. A deep copy creates
an independent duplicate.
37. How does the object differ from a plain object?
Try it Yourself >>
Try it Yourself >>
Map
Ans:
Try it Yourself >>
Try it Yourself >>
has function scope and is hoisted. and have block scope. cannot be reassigned.
Ans: Map is better for storing key-value pairs because it preserves order and allows any JavaScript Data
Typesas keys.
One
2
var
15 25 30
var a = 10; let
b = 20; const
c = 30;
// Reassigning a = 15; // OK b = 25; //
OK // c = 35; // Error console.log(a,
b, c); // 15 25 30
let map = new Map(); map.set(1,
"One"); map.set("two", 2);
console.log(map.get(1)); // One
console.log(map.get("two")); // 2
let const const
Output
Output
38. What is the difference between
39. What are generators in JavaScript?
, , and
var let
const?
Try it Yourself >>
Ans: Generators are special functions that can pause and resume their execution using
Ans: Memoization is a technique to optimize functions by caching their results for specific inputs.
First
Second
Done
function memoize(fn) {
let cache = {};
return function (n) {
if (cache[n]) {
return cache[n];
}
cache[n] = fn(n);
return cache[n];
};
}
function square(n) {
return n * n;
}
let memoizedSquare = memoize(square);
function* generatorFunction() {
yield "First";
yield "Second";
return "Done";
}
let gen = generatorFunction();
console.log(gen.next().value); // First
console.log(gen.next().value); // Second
console.log(gen.next().value); // Done
yield.
Output
40. What is memoization, and how is it implemented?
Output
Output
2
5
2
5
Developer
this
let weakMap = new WeakMap();
let obj = { name: "Arjun" };
weakMap.set(obj, "Developer");
console.log(weakMap.get(obj)); // Developer
let obj = {
name: "Maya",
greet: () => console.log("Hello, " + this.name),
};
obj.greet(); // Hello, undefined
console.log(memoizedSquare(5)); // 25 (calculated)
console.log(memoizedSquare(5)); // 25 (cached)
42. How does work in arrow functions?
41. What are WeakMaps and WeakSets in JavaScript?
Ans: WeakMaps and WeakSets are collections where keys and values are weakly referenced. They are useful
for caching without preventing garbage collection.
Try it Yourself >>
Try it Yourself >>
Ans: In arrow functions,
Read More: Garbage Collection in C#
is lexically bound to the enclosing context and does not change.
this
Output
Output
6
Hello, undefined
function curry(a) {
return function (b) {
return function (c) {
return a + b + c;
};
};
}
console.log(curry(1)(2)(3)); // 6
function debounce(func, delay) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => func(...args), delay);
};
}
43. What is currying in JavaScript?
Ans: Currying is the process of transforming a function with multiple arguments into a series of functions
that each take one argument.
44. How do you debounce a function?
Ans: Debouncing delays a function's execution until after a specified time has passed since the last
invocation.
Try it Yourself >>
Try it Yourself >>
Output
Output
Hello
Debounced
Symbol
let sym = Symbol("unique");
let obj = { [sym]: "Hello" };
console.log(obj[sym]); // Hello
const log = debounce(() => console.log("Debounced"), 1000);
log(); log(); log(); // Only executes once after 1 second
let handler = {
get: function (target, property) {
return property in target ? target[property] : "Property not found";
},
};
let obj = { name: "Ravi" };
let proxy = new Proxy(obj, handler);
45. What is the purpose of the
46. What is a JavaScript Proxy, and how is it used?
Ans: A Proxy allows you to create custom behavior for basic operations on objects like getting or setting
proper ties.
type in JavaScript?
Symbol
Ans:
Try it Yourself >>
Try it Yourself >>
is used to create unique identifiers that avoid property name collisions in objects.
Output
Output
async
call()
bind()
Data fetched
Ravi
Property not found
await
function greet(greeting) {
console.log(greeting + ", " + this.name);
}
let user = { name: "Vikram" };
console.log(proxy.name); // Ravi
console.log(proxy.age); // Property not found
apply()
async function fetchData() {
let promise = new Promise((resolve) => setTimeout(() => resolve("Data fetched"), 1000));
let result = await promise;
console.log(result);
}
fetchData();
47. What are async/await in JavaScript?
Ans:
48. What is the difference between call, apply, and bind?
Ans:
code.
and
Try it Yourself >>
Try it Yourself >>
invokes a function with arguments passed individually.
creates a new function with a specific this.
simplify working with promises, making asynchronous code look like synchronous
passes arguments as an array.
Output
Output
Hello, Vikram
Hi, Vikram
Namaste, Vikram
// Using apply
greet.apply(user, ["Hi"]); // Hi, Vikram
.catch()
// Using call
greet.call(user, "Hello"); // Hello, Vikram
// Using bind
let boundGreet = greet.bind(user, "Namaste");
boundGreet(); // Namaste, Vikram
// Using try...catch
async function fetchData() {
try {
let promise = Promise.reject("Failed to fetch");
await promise;
} catch (error) {
}
}
fetchData();
console.log(error); // Failed to fetch
try...catch
// Using .catch()
let promise = new Promise((_, reject) => reject("Error occurred"));
promise.catch((error) => console.log(error)); // Error occurred
49. How do you handle errors in promises?
Ans: Errors in promises are handled using
Try it Yourself >>
Try it Yourself >>
or with async/await.
Error occurred
Failed to fetch
Service Worker Registered
// Registering a Service Worker
if ("serviceWorker" in navigator) {
navigator.serviceWorker
}
.register("/sw.js")
.then(() => console.log("Service Worker Registered"))
.catch((error) => console.log("Registration failed:", error));
50. What is a Service Worker in JavaScript?
Ans: A Service Worker is a script that runs in the background, separate from the web page, enabling features
like offline support and caching.
Try it Yourself >>
Read More: JavaScript Interview Questions & Answers
This tutorial covers the top 50 OOPS JavaScript interview questions and answers, organized by experience
levels: fresher, intermediate, and experienced. It provides a clear understanding of core OOPS concepts in
JavaScript, such as classes, objects, inheritance, encapsulation, and polymorphism, along with practical
examples. By reviewing these questions, you’ll be ready to excel in interviews and demonstrate your expertise
in OOPS using JavaScript.
Boost your JavaScript knowledge with Scholarhat's JavaScript Course! Enroll now and master OOPS
principles to build clean and maintainable applications.
Output
Summary