Computed property names

Property names that are evaluated during runtime are called computed property names. An expression is usually resolved to find the property name dynamically. Computed properties were once defined in this way:

var object = {};object["first"+"Name"] = "Eden";//"firstName" is the property name//extractconsole.log(object["first"+"Name"]); //Output "Eden"

Here, after creating the object, we attach the properties to the object. But in ES6, we can add the properties with the computed name while creating the object. The following example demonstrates this:

let object = {["first" + "Name"]: "Eden",};//extractconsole.log(object["first" + "Name"]); //Output "Eden"

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.