Every JavaScript object has a prototype. When a property or method is not found on the object itself, JavaScript looks for it in the prototype chain. These questions cover the most common prototype interview patterns.
Question 1
function User() {}
console.log(typeof User.prototype);
Every function has a prototype property and its type is object.
Question 2
function User() {}
console.log(
User.prototype.constructor === User
);
By default, a function's prototype contains a constructor reference back to the function.
Question 3
function User() {}
const user = new User();
console.log(
user.__proto__ === User.prototype
);
The instance links to the constructor's prototype.
Question 4
function User() {}
const user = new User();
console.log(
Object.getPrototypeOf(user) === User.prototype
);
Object.getPrototypeOf() returns the object's prototype.
Question 5
function User() {}
User.prototype.role = "Admin";
const user = new User();
console.log(user.role);
The property is found through prototype lookup.
Question 6
function User() {}
User.prototype.role = "Admin";
const user = new User();
console.log(
user.hasOwnProperty("role")
);
The property exists on the prototype, not on the object itself.
Question 7
function User() {}
User.prototype.role = "Admin";
const user = new User();
user.role = "Manager";
console.log(user.role);
The own property shadows the prototype property.
Question 8
function User() {}
User.prototype.role = "Admin";
const user = new User();
user.role = "Manager";
console.log(
user.hasOwnProperty("role")
);
The assignment creates an own property.
Question 9
const person = {
role: "Admin"
};
const user = Object.create(person);
console.log(user.role);
user inherits from person.
Question 10
const person = {
role: "Admin"
};
const user = Object.create(person);
console.log(
user.hasOwnProperty("role")
);
The property belongs to the prototype object.
Question 11
const person = {
role: "Admin"
};
const user = Object.create(person);
user.role = "Manager";
console.log(user.role);
The own property shadows the inherited property.
Question 12
const person = {
role: "Admin"
};
const user = Object.create(person);
console.log(
Object.getPrototypeOf(user) === person
);
Object.create() sets the prototype directly.
Question 13
function User() {}
User.prototype.sayHello = function() {
return "Hello";
};
const user = new User();
console.log(user.sayHello());
Methods can be shared through the prototype.
Question 14
function User() {}
User.prototype.sayHello = function() {
return "Hello";
};
const u1 = new User();
const u2 = new User();
console.log(
u1.sayHello === u2.sayHello
);
Both instances share the same prototype method.
Question 15
function User() {}
const user = new User();
User.prototype.role = "Admin";
console.log(user.role);
Prototype updates affect existing instances.
Question 16
function User() {}
const user = new User();
console.log(
user.constructor === User
);
The constructor reference comes from the prototype.
Question 17
function User() {}
const user = new User();
console.log(
typeof user.__proto__
);
The prototype object is an object.
Question 18
const arr = [];
console.log(
Array.prototype === arr.__proto__
);
Arrays inherit from Array.prototype.
Question 19
const obj = {};
console.log(
Object.getPrototypeOf(obj) === Object.prototype
);
Plain objects inherit from Object.prototype.
Question 20
function User() {}
const user = new User();
console.log(
"toString" in user
);
toString is inherited through the prototype chain from Object.prototype.
Question 21
function User(name) {
this.name = name;
}
User.prototype.getName = function() {
return this.name;
};
const user = new User("John");
console.log(user.getName());
The instance finds getName() on the prototype.
Question 22
function User() {}
User.prototype.role = "Admin";
const u1 = new User();
const u2 = new User();
console.log(u1.role === u2.role);
Both instances read the same prototype property.
Question 23
function User() {}
User.prototype.role = "Admin";
const user = new User();
delete user.role;
console.log(user.role);
The property comes from the prototype.
Question 24
function User() {}
User.prototype.role = "Admin";
const user = new User();
user.role = "Manager";
delete user.role;
console.log(user.role);
Deleting the own property exposes the prototype property again.
Question 25
function User() {}
User.prototype.role = "Admin";
const user = new User();
console.log(
"role" in user
);
The in operator checks the entire prototype chain.
Question 26
function User() {}
User.prototype.role = "Admin";
const user = new User();
console.log(
user.hasOwnProperty("role")
);
hasOwnProperty() checks only the object's own properties.
Question 27
function User() {}
User.prototype.role = "Admin";
const user = new User();
user.role = "Manager";
console.log(
user.hasOwnProperty("role")
);
Assigning creates an own property.
Question 28
function User() {}
User.prototype.sayHi = function() {
return "Hi";
};
const user = new User();
console.log(
typeof user.sayHi
);
Prototype methods are functions.
Question 29
function User() {}
const u1 = new User();
User.prototype.role = "Admin";
const u2 = new User();
console.log(u1.role, u2.role);
Existing instances also see prototype updates.
Question 30
function User() {}
const u1 = new User();
User.prototype = {
role: "Admin"
};
const u2 = new User();
console.log(u1.role, u2.role);
Replacing the prototype affects only future instances.
Question 31
function User() {}
User.prototype.role = "Admin";
const user = new User();
User.prototype.role = "Manager";
console.log(user.role);
The instance reads the current prototype value.
Question 32
function User() {}
User.prototype = {
role: "Admin"
};
const user = new User();
console.log(
user.constructor === User
);
Replacing the prototype removes the default constructor reference.
Question 33
function User() {}
User.prototype = {
constructor: User
};
const user = new User();
console.log(
user.constructor === User
);
The constructor reference was restored manually.
Question 34
function Animal() {}
Animal.prototype.sound = function() {
return "Sound";
};
function Dog() {}
Dog.prototype = Object.create(
Animal.prototype
);
const dog = new Dog();
console.log(dog.sound());
Dog inherits from Animal through the prototype chain.
Question 35
function Animal() {}
Animal.prototype.type = "Animal";
function Dog() {}
Dog.prototype = Object.create(
Animal.prototype
);
const dog = new Dog();
console.log(dog.type);
The property is inherited from Animal.prototype.
Question 36
function Animal() {}
Animal.prototype.type = "Animal";
function Dog() {}
Dog.prototype = Object.create(
Animal.prototype
);
Dog.prototype.type = "Dog";
const dog = new Dog();
console.log(dog.type);
The child prototype overrides the parent property.
Question 37
function Animal() {}
Animal.prototype.sound = function() {
return "Animal";
};
function Dog() {}
Dog.prototype = Object.create(
Animal.prototype
);
Dog.prototype.sound = function() {
return "Dog";
};
const dog = new Dog();
console.log(dog.sound());
The child method overrides the parent method.
Question 38
function Animal() {}
function Dog() {}
Dog.prototype = Object.create(
Animal.prototype
);
const dog = new Dog();
console.log(
dog instanceof Animal
);
Animal.prototype exists in the prototype chain.
Question 39
function Animal() {}
function Dog() {}
Dog.prototype = Object.create(
Animal.prototype
);
const dog = new Dog();
console.log(
dog instanceof Dog
);
Dog.prototype is also in the prototype chain.
Question 40
function Animal() {}
function Dog() {}
Dog.prototype = Object.create(
Animal.prototype
);
const dog = new Dog();
console.log(
Object.getPrototypeOf(
Object.getPrototypeOf(dog)
) === Animal.prototype
);
The second prototype level points to Animal.prototype.
Question 41
const arr = [];
console.log(
arr instanceof Array
);
Array.prototype exists in the prototype chain.
Question 42
const arr = [];
console.log(
arr instanceof Object
);
Arrays ultimately inherit from Object.prototype.
Question 43
console.log(
Array.prototype.__proto__ === Object.prototype
);
Array.prototype inherits from Object.prototype.
Question 44
console.log(
Object.prototype.__proto__
);
Object.prototype is the top of the standard prototype chain.
Question 45
function User() {}
console.log(
User.__proto__ === Function.prototype
);
Functions are objects created by Function.
Question 46
function User() {}
console.log(
User.prototype.__proto__ === Object.prototype
);
A function's prototype object inherits from Object.prototype.
Question 47
const obj = {};
console.log(
obj instanceof Object
);
Object.prototype exists in the chain.
Question 48
function User() {}
const user = new User();
console.log(
user instanceof User
);
User.prototype is found in the prototype chain.
Question 49
const person = {
role: "Admin"
};
const user = Object.create(person);
console.log(
user instanceof Object
);
The chain eventually reaches Object.prototype.
Question 50
const person = {
role: "Admin"
};
const user = Object.create(person);
console.log(
person.isPrototypeOf(user)
);
person directly acts as the prototype of user.
Question 51
const arr = [];
console.log(
typeof arr.push
);
push() comes from Array.prototype.
Question 52
const arr = [];
console.log(
arr.hasOwnProperty("push")
);
push is inherited from Array.prototype.
Question 53
Array.prototype.sayHi = function() {
return "Hi";
};
const arr = [];
console.log(arr.sayHi());
All arrays inherit methods added to Array.prototype.
Question 54
Array.prototype.sayHi = function() {
return "Hi";
};
const a = [];
const b = [];
console.log(
a.sayHi === b.sayHi
);
Both arrays share the same prototype method.
Question 55
function User() {}
const user = new User();
console.log(
User.prototype.isPrototypeOf(user)
);
User.prototype exists in the object's prototype chain.
Question 56
console.log(
Function.prototype.__proto__ === Object.prototype
);
Function.prototype also inherits from Object.prototype.
Question 57
function User() {}
console.log(
User instanceof Function
);
Functions are instances of Function.
Question 58
console.log(
Function instanceof Function
);
This is one of JavaScript's famous prototype-chain quirks.
Question 59
console.log(
Object instanceof Function
);
Object itself is a function object.
Question 60
console.log(
Object instanceof Object
);
Object's prototype chain eventually satisfies the instanceof check.
This is another famous JavaScript interview question.