Let us look at an example where we assign variables as the values of properties with the same names.
This code above works, but writing seatingCapacity: seatingCapacity
and fuelType: fuelType
can be repetitive.
If the key
and the value
of a property are the same, you can use the shorthand syntax by simply writing the variable name. The previous code can be rewritten as:
Here, JavaScript automatically uses the variable name as the property name and assigns its value.
Let us look at another example with all properties using shorthand.
The shorthand syntax makes the code easier to read and maintain.