Object.defineProperty() — JS Technical Interview Questions & Answers
Adds or configures a single property with fine-grained control.
Object.defineProperty()
- Object.defineProperty() is a JS topic. In plain words you use it for { name, marks } in a student record. Don’t start with a slogan — start with that picture.
- Smallest example: const { name } = student. Type it, run it, and say what you see. If you can do that from memory, you know Object.defineProperty().
- From the example next to this theory: Adds or configures a single property with fine-grained control.
- Trap: copying nested objects by accident (shallow copy). Fix that before you talk about advanced JS.
- Viva: what is Object.defineProperty()? Then show const { name } = student. Then name the trap.
Technical Interview Tip: What is Object.defineProperty()? Show this: const { name } = student. Trap: copying nested objects by accident (shallow copy).
Code Examples for Object.defineProperty()
Object.defineProperty() — working example
// Topic: Object.defineProperty()
const nums = [1, 2, 3];
const doubled = nums.map((n) => n * 2);
console.log(doubled);
console.log(doubled.reduce((a, b) => a + b, 0));Object.defineProperty(): map then reduce on [1, 2, 3]. Say both prints.
How to Explain Object.defineProperty() to an Interviewer
What is Object.defineProperty()?
Used for { name, marks } a student record Not just the heading
Give one small example of Object.defineProperty().
const { name } = student Adds or configures a single property with fine-grained control
What mistake do beginners make with Object.defineProperty()?
copying nested objects by accident (shallow copy) Show the fix
Where do you use Object.defineProperty()?
a student record { name, marks }