Get an Object's Value using a Variable Key in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
5 min

banner

# Table of Contents

  1. Get an Object's Value using a Variable Key in JavaScript
  2. Access an Object Property with a Hyphen or Space in JavaScript

# Get an Object's Value using a Variable Key in JavaScript

Use bracket notation to get an object's value by a variable key, e.g. obj[myVar].

The variable or expression in the brackets gets evaluated, so if a key with the computed name exists, you will get the corresponding value back.

index.js
const obj = { country: 'Chile', name: 'bobby hadz', }; // โœ… Get object value by variable key const myVar = 'country'; console.log(obj[myVar]); // ๐Ÿ‘‰๏ธ "Chile" // โœ… Set object key by variable const num = 4; obj['myKey' + num] = 'someValue'; console.log(obj.myKey4); // ๐Ÿ‘‰๏ธ "someValue"

get object value using variable key

The code for this article is available on GitHub

We can access an object's properties using:

  • bracket notation
  • dot notation
index.js
const obj = { country: 'Chile', name: 'bobby hadz', }; // โœ… Using bracket notation with string console.log(obj['name']); // ๐Ÿ‘‰๏ธ "bobby hadz" // โœ… Using dot notation console.log(obj.name); // ๐Ÿ‘‰๏ธ "bobby hadz"
You have to use the bracket [] notation syntax when a variable has to be evaluated to a specific key.

When using dot notation, the property name after the dot is taken literally and is not evaluated, even if a variable with the specified name exists.

index.js
const obj = { country: 'Chile', name: 'bobby hadz', }; const myVar = 'country'; console.log(obj.myVar); // ๐Ÿ‘‰๏ธ undefined

The object doesn't have a myVar key, so trying to access it returns undefined.

Instead, use bracket notation to evaluate the variable when accessing the corresponding key.

index.js
const obj = { country: 'Chile', name: 'bobby hadz', }; const myVar = 'country'; console.log(obj[myVar]); // ๐Ÿ‘‰๏ธ "Chile"

The variable between the brackets gets evaluated before accessing the object at the specified key.

# Get an object's value using an expression

You can also use the addition (+) operator or any other expression you can think of, to compute the key.

index.js
const obj = { country: 'Chile', name: 'bobby hadz', }; const myVar = 'c'; console.log(obj[myVar + 'ountry']); // ๐Ÿ‘‰๏ธ "Chile"

get object value using an expression

The code for this article is available on GitHub

Here's an example of using the ternary operator, which is very similar to an if/else statement.

index.js
const obj = { country: 'Chile', name: 'bobby hadz', }; const myVar1 = 'country'; const myVar2 = 'name'; console.log(obj[5 > 0 ? myVar1 : myVar2]); // ๐Ÿ‘‰๏ธ "Chile"
If the expression to the left of the question mark is truthy, the operator returns the value to the left of the colon, otherwise, the value to the right of the colon is returned.
index.js
const myVar1 = 'country'; const myVar2 = 'name'; // ๐Ÿ‘‡๏ธ country console.log(5 > 0 ? myVar1 : myVar2);

After the expression gets evaluated, it returns the value of the myVar1 variable.

# Setting object keys by using a variable

You can also use the computed property names approach when setting keys on an object.

index.js
const obj = { country: 'Chile', name: 'bobby hadz', }; const myVar = 'age'; obj[myVar] = 30; console.log(obj[myVar]); // ๐Ÿ‘‰๏ธ 30

setting object keys by using variable

The code for this article is available on GitHub

We used a variable to set the age key on the object and then used bracket notation to evaluate the variable to access the key.

# Access an Object Property with a Hyphen or Space in JavaScript

Use bracket notation to access an object property with a hyphen or a space, e.g. obj['with-hyphen'].

If the object's property contains hyphens, spaces, special characters or starts with an integer, a hyphen has to be used when accessing it.

index.js
const obj = { 'with-hyphen': 'value1', 'with space': 'value2', 'with.symbol': 'value3', '123hello': 'value4', }; console.log(obj['with-hyphen']); // ๐Ÿ‘‰๏ธ "value1" console.log(obj['with space']); // ๐Ÿ‘‰๏ธ "value2" console.log(obj['with.symbol']); // ๐Ÿ‘‰๏ธ "value3" console.log(obj['123hello']); // ๐Ÿ‘‰๏ธ "value4"

access object property with hyphen or space

The code for this article is available on GitHub

The same syntax can be used to update an object property that contains a hyphen or a space.

index.js
const obj = { 'with-hyphen': 'value1', 'with space': 'value2', 'with.symbol': 'value3', '123hello': 'value4', }; obj['with space'] = 'new value'; console.log(obj['with space']); // ๐Ÿ‘‰๏ธ new value

update object property that contains hyphen or space

We used bracket notation to access an object property that contains a hyphen.

There are two ways to access properties in an object:

  1. dot notation
index.js
const obj = {name: 'Bobby'}; console.log(obj.name); // ๐Ÿ‘‰๏ธ Bobby obj.name = 'Alice'; console.log(obj); // ๐Ÿ‘‰๏ธ { name: 'Alice' }
  1. bracket notation
index.js
const obj = {'first name': 'Bobby'}; console.log(obj['first name']); // ๐Ÿ‘‰๏ธ "Bobby" obj['first name'] = 'Alice'; console.log(obj); // ๐Ÿ‘‰๏ธ {'first name': 'Alice'}
The code for this article is available on GitHub

Most developers use dot notation to access object properties unless the property contains spaces, hyphens or starts with a number or a special character.

You can use bracket notation in all cases where dot notation can be used, however, you can't use dot notation if the object's key contains spaces, hyphens, or starts with a number or a special character.

In all of the aforementioned scenarios, you have to use the bracket [] notation syntax.

index.js
const obj = {'first name': 'Bobby'}; console.log(obj['first name']); // ๐Ÿ‘‰๏ธ Bobby

Similarly to reading the value of a key that contains a space or a hyphen, we can use bracket notation to update the value.

index.js
const obj = {'first name': 'Bobby'}; obj['first name'] = 'Alice'; console.log(obj['first name']); // ๐Ÿ‘‰๏ธ Alice

# CSS properties get converted to camelCase

The names of CSS properties are camelCased when accessed in your JavaScript code.

index.js
const box = document.getElementById('box'); box.style.marginLeft = '10px';

For example, properties like margin-left and margin-top become marginLeft and marginTop.

# Use bracket notation when the key is stored in a variable

You also have to use bracket notation when the key is stored in a variable.

index.js
const obj = { country: 'Chile', }; const key = 'country'; console.log(obj[key]); // ๐Ÿ‘‰๏ธ "Chile"

use bracket notation when key is stored in variable

The code for this article is available on GitHub

If we were to access the property by using obj.key we would be accessing a property named key on the object.

index.js
const obj = { country: 'Chile', }; const key = 'country'; console.log(obj.key); // ๐Ÿ‘‰๏ธ undefined

We got an undefined value because the object doesn't have a property named key.

By using the variable between the square brackets, it gets evaluated and allows us to access the country property.

# Using dot notation where possible and bracket notation where necessary

You can also mix and match - use bracket notation where necessary and use dot notation in all other cases.

index.js
const obj = { person: { 'first name': 'Bobby', }, }; console.log(obj.person['first name']); // ๐Ÿ‘‰๏ธ "Tom" obj.person['first name'] = 'Alice'; console.log(obj); // ๐Ÿ‘‰๏ธ { person: { 'first name': 'Alice' } }

using dot notation where possible and bracket notation where necessary

The code for this article is available on GitHub

We can access the person property in the object using dot notation because it contains no spaces.

However, the first name property does, so we have to use bracket notation.

The alternative is to be consistent and use bracket notation to access both keys.

index.js
const obj = { person: { 'first name': 'Bobby', }, }; console.log(obj['person']['first name']); // ๐Ÿ‘‰๏ธ "Bobby" obj['person']['first name'] = 'Alice'; console.log(obj); // ๐Ÿ‘‰๏ธ {person: {'first name': 'Alice'}}
The code for this article is available on GitHub

In my experience, JavaScript developers use dot notation where possible and bracket notation only when necessary.

This is what most linters suggest as well, as the dot notation syntax is more concise and easier to read.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev