Object.freeze() vs Object.seal() — JS Technical Interview Questions & Answers
Locking objects against changes.
Object.freeze() vs Object.seal()
- Object.freeze() vs Object.seal() (js) 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() vs Object.seal() (js).
- From the example next to this theory: Locking objects against changes.
- Trap: copying nested objects by accident (shallow copy). Fix that before you talk about advanced JS.
- Viva: what is Object.freeze() vs Object.seal() (js)? Then show const { name } = student. Then name the trap.
Technical Interview Tip: What is Object.freeze() vs Object.seal() (js)? Show this: const { name } = student. Trap: copying nested objects by accident (shallow copy).
Code Examples for Object.freeze() vs Object.seal()
Object.freeze() vs Object.seal() — 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() vs Object.seal() — objects store keyed values; constructors make instances; getters compute; freeze/seal lock mutation.
How to Explain Object.freeze() vs Object.seal() to an Interviewer
What is Object.freeze() vs Object.seal() (js)?
Used for { name, marks } a student record Not just the heading
Give one small example of Object.freeze() vs Object.seal() (js).
const { name } = student Locking objects against changes
What mistake do beginners make with Object.freeze() vs Object.seal() (js)?
copying nested objects by accident (shallow copy) Show the fix
Where do you use Object.freeze() vs Object.seal() (js)?
a student record { name, marks }