In JavaScript, you can simplify object creation by using the shorthand property names introduced in ES6. This allows you to create objects more concisely when the property names are the same as the variable names.
ES5
const make = 'Ford';
const model = 'Mustang';
const engine = '5.0';
const car = {
make: make,
model: model,
engine: engine,
};
ES6
const make = 'Ford';
const model = 'Mustang';
const engine = '5.0';
const car = {
make,
model,
engine,
};
Explanation
- In ES5, you need to explicitly define the property names and assign them the corresponding variable values.
- In ES6, you can use shorthand property names to create the object more concisely when the property names match the variable names.
Usage
This shorthand syntax is particularly useful when you have many properties to define, making your code cleaner and easier to read.