Skip to content
the poor coder

javaScript set object key by variable

Poor Man
Published
Reading time
2 min read
On this page

How to set an object key in JavaScript using a variable#

If you want to set a JavaScript object key using a variable, there are multiple ways to achieve it. Let's explore some of them:

Method 1: Using bracket notation#

One of the easiest ways to set an object key using a variable is by using bracket notation. This method is useful when you don't know the key name in advance.

Text

let myObj = {};
let myKey = "name";
let myValue = "Raju";

myObj[myKey] = myValue;

console.log(myObj); // Output: { name: "Raju" }

In the above code, we're creating an empty object called `myObj`. Then we're creating two variables `myKey` and `myValue`, where `myKey` is the key name we want to set dynamically and `myValue` is the value we want to assign to the key.

We're then setting the value of `myObj[myKey]` to `myValue`. This sets the `name` key in `myObj` with the value `"Raju"`. Finally, we're logging the entire object to the console using `console.log()`.

Method 2: Using ES6 computed properties#

Another way to set an object key using a variable is by using ES6 computed properties. This method is useful when you want to create an object with dynamic keys.

Text

let myKey = "name";
let myValue = "Raju";

let myObj = {
  [myKey]: myValue
};

console.log(myObj); // Output: { name: "Raju" }

In the above code, we're using the ES6 computed properties syntax to create an object `myObj`. We're setting the key name dynamically by wrapping the variable `myKey` in square brackets.

We're then setting the value of the dynamically computed key to `myValue`. This creates an object with a single key-value pair `{ name: "Raju" }`. Finally, we're logging the entire object to the console using `console.log()`.

Method 3: Using Object.defineProperty()#

You can also use the `Object.defineProperty()` method to set an object key using a variable. This method is useful when you want to define additional properties for the key.

Text

let myObj = {};
let myKey = "name";
let myValue = "Raju";

Object.defineProperty(myObj, myKey, {
  value: myValue,
  writable: true,
  enumerable: true,
  configurable: true
});

console.log(myObj); // Output: { name: "Raju" }

In the above code, we're creating an empty object called `myObj`. Then we're creating two variables `myKey` and `myValue`, where `myKey` is the key name we want to set dynamically and `myValue` is the value we want to assign to the key.

We're then using the `Object.defineProperty()` method to define the property with the key name `myKey` and additional properties such as `value`, `writable`, `enumerable`, and `configurable`. This sets the `name` key in `myObj` with the value `"Raju"`. Finally, we're logging the entire object to the console using `console.log()`.

These are some of the ways to set an object key using a variable in JavaScript.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC