JS-Tips-And-Tricks icon indicating copy to clipboard operation
JS-Tips-And-Tricks copied to clipboard

Add an object key assigment from variable (faster way)

Open technic404 opened this issue 8 months ago • 0 comments

We want to pass a key to an object that is assigned to variable.

Instead of doing

const key = "b";
const obj = { 
    a: 1
};

obj[key] = "value";

We can define the object key without obj[key]

const key = "b";
const obj = { 
    a: 1, 
    [key]: "value"
};

technic404 avatar May 24 '25 19:05 technic404