Object.freeze() — JS Technical Interview Questions & Answers
Prevents adding, removing or changing properties.
Object.freeze()
- Object.freeze() 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.freeze().
- From the example next to this theory: Prevents adding, removing or changing properties.
- Trap: copying nested objects by accident (shallow copy). Fix that before you talk about advanced JS.
- Viva: what is Object.freeze()? Then show const { name } = student. Then name the trap.
Technical Interview Tip: What is Object.freeze()? Show this: const { name } = student. Trap: copying nested objects by accident (shallow copy).
Code Examples for Object.freeze()
Object.freeze() — working example
const user = {
name: "Ada",
city: "Pune",
skills: ["JS"],
get label() {
return this.name + " @" + this.city;
},
};
user.skills.push("SQL");
user["role"] = "dev";
console.log(user.label, user.skills);
function Person(name) {
this.name = name;
}
const p = new Person("Lin");
console.log(p.name);
const config = Object.freeze({ theme: "light" });
// config.theme = "dark"; // TypeError in strict mode
console.log(config.theme);Object.freeze() — objects store keyed values; constructors make instances; getters compute; freeze/seal lock mutation.
How to Explain Object.freeze() to an Interviewer
What is Object.freeze()?
Used for { name, marks } a student record Not just the heading
Give one small example of Object.freeze().
const { name } = student Prevents adding, removing or changing properties
What mistake do beginners make with Object.freeze()?
copying nested objects by accident (shallow copy) Show the fix
Where do you use Object.freeze()?
a student record { name, marks }