Nearby lessons

8 of 22

Advanced JavaScript: Object Literals

📌 What are Object Literals?

Object literals are a convenient way to create objects in JavaScript. With modern ES6 syntax, object creation is cleaner and more concise, especially when keys and variable names are the same or when using computed property names.

1️⃣ Shorthand Property Names

If the key and variable name are the same, you can omit the key name. JavaScript will automatically use the variable name as the key.

Example02
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

2️⃣ Computed Property Names

You can use a variable or expression inside square brackets [] to define a property name dynamically.

Example03
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

3️⃣ Using Variables as Property Names

Instead of static keys, you can use variables as dynamic keys in object literals using square brackets.

Example04
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

4️⃣ Method Definitions (Old vs New Syntax)

In ES6, you can define methods without the function keyword. It's shorter and more readable.

Example05
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3