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