JavaScript Interview Question #106

What is the difference between an own property and an inherited property?

Closures, Prototypes, Classes & Advanced Functions Mid-Level Intermediate

Quick Interview Answer

An own property belongs directly to the object, while an inherited property is found through the object's prototype chain. The in operator checks both own and inherited properties.

Detailed Explanation

An own property belongs directly to the object, while an inherited property is found through the object's prototype chain. The in operator checks both own and inherited properties.

Object.hasOwn is the modern direct way to check whether a property belongs to the object itself. This distinction is important when iterating, validating objects, or working with prototype-based inheritance.

Code Example

const parent = { role: "User" };
const user = Object.create(parent);
user.name = "Asif";

console.log("name" in user); // true
console.log("role" in user); // true

console.log(Object.hasOwn(user, "name")); // true
console.log(Object.hasOwn(user, "role")); // false