Enhanced Object Literals
Enhanced Object Literals in ES6
ECMAScript 2015 introduced several syntactic enhancements to object literal notation, making object creation concise, expressive, and less repetitive.
1. Property Value Shorthand
Before ES6, assigning a variable to an object property with the same name required duplicating the identifier:
If the property name matches the variable name, write it once!
2. Method Definition Shorthand
You can define methods without typing the function keyword or colon : :
3. Computed Property Names ([expression])
Before ES6, to create an object whose property key was dynamic or computed from an expression, you had to instantiate the object first and then use bracket notation on a separate line:
With ES6 Computed Property Names:
You can calculate the property key directly inside the object literal using square brackets [...]:
4. Real-World Use Case: Dynamic Form Handlers
Computed properties are universally used in form handlers (like React onChange events):
Practice Quiz
{ title: title }?greet inside an object?const key = "marks"; const obj = { [key]: 95 };, what is the output of obj.marks?Arrow Functions Advanced
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Default Parameters | Arrow Functions Advanced |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.