Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Computed Property Names in JavaScript

The Computed Property Name feature allows us to use an expression that returns a valid value as Property Name. We need to enclose the expression inside square brackets( []).

Dynamic Property Name

In the following example, we create a variable author and store Author Name in it.

While creating the book object, we use the author variable inside the []. The JavaScript creates the property Author Name.

This is how dynamic properties were created before ES6.

        
let author = 'Author Name';
 
 
let book = {
    [author]: 'Marijn Haverbeke',
};
 
 
console.log(book[author]); // Marijn Haverbeke

        
    

Computed Property Names

Since ES6, we can use the expression directly inside the []. The expression must return a single value. The JavaScript will evaluate the expression and uses the return value to create the property.

        

let prefix = "Book";
 
 let book = {
     [ prefix + "Author"]: 'Marijn Haverbeke',
     [ prefix + "Title"]: "Eloquent JavaScript"
 };
  
 console.log(book[prefix + "Author"]); // Marijn Haverbeke
 console.log(book["BookAuthor"]);      // Marijn Haverbeke
  
 //We can also use this. because there is no space in the property name
 console.log(book.BookAuthor);        // Marijn Haverbeke

        
    

You can also invoke a function inside square brackets.

        
let prefix = "Book";
 
let book = {
    [ getAuthorName()]: 'Marijn Haverbeke',
    [ prefix + "Title"]: "Eloquent JavaScript"
};
 
function getAuthorName() {
  return prefix+'Author'
}
 

        
    

The following example, dynamically adds the property to object a

        
let i = 0
let a = {};
  
for (i=0; i< 5;i++) {
    a ['foo'+i]=i]
}
 
console.log(a)   //{foo0: 0, foo1: 1, foo2: 2, foo3: 3, foo4: 4}
 

        
    

The addProperty takes any object and adds the property passed in its argument.

        
person = {}
 
 
function addProperty(obj, key, value) {
    obj[key] = value
}
 
addProperty(person, 'firstName', 'Allie') 
addProperty(person, 'lastName', 'Grater') 
 
 
console.log(person)  //{firstName: "Allie", lastName: "Grater"}
 

        
    

Read More

  1. JavaScript Tutorial
  2. Functions in JavaScript
  3. Pass By Value & Pass By Reference
  4. Function Expression
  5. Nested Functions
  6. Immediately-invoked Function Expressions (IIFE)
  7. Call functions
  8. Arrow functions
  9. Arguments Object
  10. Rest Parameters