Post cover

3 Ways To Access Object Properties in JavaScript

You can access the properties of an object in JavaScript in 3 ways:

  1. Dot property accessor: object.property
  2. Square brackets property access: object['property']
  3. Object destructuring: const { property } = object

Let's see how each syntax to access the properties work. And understand when it's reasonable, depending on the situation, to use one way or another.

1. Dot property accessor

A common way to access the property of an object is the dot property accessor syntax:

javascript
expression.identifier

expression should evaluate to an object, and identifier is the name of the property you'd like to access.

For example, let's access the property name of the object hero:

javascript
const hero = {
name: 'Batman'
};
// Dot property accessor
hero.name; // => 'Batman'

hero.name is a dot property accessor that reads the property name of the object hero.

You can use the dot property accessor in a chain to access deeper properties: object.prop1.prop2.

Choose the dot property accessor when the property name is known ahead of time.

1.1 Dot property accessor requires identifiers

The dot property accessor works correctly when the property name is a valid identifier. An identifier in JavaScript contains Unicode letters, $, _, and digits 0..9, but cannot start with a digit.

This is not a problem, because usually, the property names are valid identifiers: e.g. name, address, street, createdBy.

But sometimes properties are not valid identifiers:

javascript
const weirdObject = {
'prop-3': 'three',
'3': 'three'
};
weirdObject.prop-3; // => NaN
weirdObject.3; // throws SyntaxError: Unexpected number

Because prop-3 and 3 are invalid identifiers, the dot property accessor doesn't work:

  • weirdObject.prop-3 evaluates to NaN, instead of the expected 'tree'
  • weirdObject.3 throws a SyntaxError!

Why does the expression weirdObject.prop-3 evaluate to NaN? Please write your answer in a comment below!

To access the properties with these special names, use the square brackets property accessor (which is described in the next section):

javascript
const weirdObject = {
'prop-3': 'three',
'3': 'three'
};
weirdObject['prop-3']; // => 'three'
weirdObject[3]; // => 'three'

The square brackets syntax accesses without problems the properties that have special names: weirdObject['prop-3'] and weirdObject[3].

2. Square brackets property accessor

The square brackets property accessor has the following syntax:

javascript
expression[expression]

The first expression should evaluate to an object and the second expression should evaluate to a string denoting the property name.

Here's an example:

javascript
const property = 'name';
const hero = {
name: 'Batman'
};
// Square brackets property accessor:
hero['name']; // => 'Batman'
hero[property]; // => 'Batman'

hero['name'] and hero[property] both read the property name by using the square brackets syntax.

Choose the square brackets property accessor when the property name is dynamic, i.e. determined at runtime.

3. Object destructuring

The basic object destructuring syntax is pretty simple:

javascript
const { identifier } = expression;

identifier is the name of the property to access and expression should evaluate to an object. After the destructuring, the variable identifier contains the property value.

Here's an example:

javascript
const hero = {
name: 'Batman'
};
// Object destructuring:
const { name } = hero;
name; // => 'Batman'

const { name } = hero is an object destructuring. The destructuring defines a variable name with the value of property name.

When you get used to object destructuring, you will find that its syntax is a great way to extract the properties into variables.

Choose the object destructuring when you'd like to create a variable having the property value.

Note that you can extract as many properties as you'd like:

javascript
const { identifier1, identifier2, .., identifierN } = expression;

3.1 Alias variable

If you'd like to access the property, but create a variable name different than the property name, you could use aliasing.

javascript
const { identifier: aliasIdentifier } = expression;

identifier is the name of the property to access, aliasIdentifier is the variable name, and expression should evaluate to an object. After the destructuring, the variable aliasIdentifier contains the property value.

Here's an example:

javascript
const hero = {
name: 'Batman'
};
// Object destructuring:
const { name: heroName } = hero;
heroName; // => 'Batman'

const { name: heroName } = hero is an object destructuring. The destucturing defines a new variable heroName (instead of name as in previous example), and assigns to heroName the value hero.name.

3.2 Dynamic property name

What makes the object destructuring even more useful is that you could extract to variables properties with the dynamic value:

javascript
const { [expression]: identifier } = expression;

The first expression should evaluate to a property name, and the identifier should indicate the variable name created after the destructuring. The second expression should evaluate to the object you'd like to destructure.

Here's an example:

javascript
const property = 'name';
const hero = {
name: 'Batman'
};
// Object destructuring:
const { [property]: name } = hero;
name; // => 'Batman'

const { [property]: name } = hero is an object destructuring that dynamically, at runtime, determines what property to extract.

4. When the property doesn't exist

If the accessed property doesn't exist, all 3 accessor syntaxes evalute to undefined:

javascript
const hero = {
characterName: 'Batman'
};
hero.name; // => undefined
hero['name']; // => undefined
const { name } = hero;
name; // => undefined

The property name doesn't exist in the object hero. Thus the dot property accessor hero.name, square brackets property accessor hero['name'] and the variable name after destructuring evaluate to undefined.

5. Conclusion

JavaScript provides a bunch of good ways to access object properties.

The dot property accessor syntax object.property works nicely when you know the variable ahead of time.

When the property name is dynamic or is not a valid identifier, a better alternative is square brackets property accessor: object[propertyName].

The object destructuring extracts the property directly into a variable: { property } = object. Moreover, you can extract the dynamic property names (determined at runtime): { [propertName]: variable } = object.

There are no good or bad ways to access properties. Choose depending on your particular situation.

Like the post? Please share!

Quality posts into your inbox

I regularly publish posts containing:

  • Important JavaScript concepts explained in simple words
  • Overview of new JavaScript features
  • How to use TypeScript and typing
  • Software design and good coding practices

Subscribe to my newsletter to get them right into your inbox.

Join 4716 other subscribers.
Dmitri Pavlutin

About Dmitri Pavlutin

Tech writer and coach. My daily routine consists of (but not limited to) drinking coffee, coding, writing, coaching, overcoming boredom 😉.
Email addressTwitter profileFacebook pageLinkedIn profile