The differences between Dot Notation and Bracket Notation in Javascript

Max Headway
3 min readFeb 19, 2023

--

In JavaScript, objects are used to store data in key-value pairs, and accessing these values can be done through two different notations: dot notation and bracket notation. In this article, we’ll take a look at the differences between these two notations and their pros and cons.

Dot Notation:

Dot notation is the most commonly used notation for accessing object properties. In dot notation, you access an object’s property by specifying the object followed by a period and the property name. Here’s an example:

const person = {
name: 'John',
age: 25,
address: {
city: 'New York',
state: 'NY'
}
};

console.log(person.name); // Output: John
console.log(person.address.city); // Output: New York

In the above example, we are accessing the name property of the person object using dot notation. We are also accessing the city property of the address object, which is a property of the person object.

Pros of Dot Notation:

  • Dot notation is easy to read and understand, especially for developers who are familiar with object-oriented programming.
  • It provides a clean and concise syntax, making it easier to write and maintain code.
  • Dot notation is faster than bracket notation, as it doesn’t require any additional processing.

Cons of Dot Notation:

  • Dot notation cannot be used for properties with special characters or spaces in their names.
  • It cannot be used for accessing properties dynamically.

Bracket Notation:

Bracket notation is an alternative way of accessing object properties. In bracket notation, you access an object’s property by specifying the object followed by square brackets and the property name inside quotes. Here’s an example:

const person = {
name: 'John',
age: 25,
address: {
city: 'New York',
state: 'NY'
}
};

console.log(person['name']); // Output: John
console.log(person['address']['city']); // Output: New York

In the above example, we are accessing the name property of the person object using bracket notation. We are also accessing the city property of the address object, which is a property of the person object.

Pros of Bracket Notation:

  • Bracket notation can be used for accessing properties dynamically, as the property name can be passed as a variable.
  • It can be used for accessing properties with special characters or spaces in their names.

Cons of Bracket Notation:

  • Bracket notation is slightly slower than dot notation, as it requires additional processing to evaluate the property name.
  • It can be more verbose and harder to read, especially for developers who are not familiar with the syntax.

Code Example:

Here’s an example of using bracket notation to access a property dynamically:

const person = {
name: 'John',
age: 25,
address: {
city: 'New York',
state: 'NY'
}
};

const propertyName = 'name';
console.log(person[propertyName]); // Output: John

In the above example, we are storing the property name in a variable and using it to access the name property of the person object using bracket notation.

Conclusion:

In conclusion, both dot notation and bracket notation have their pros and cons, and the choice of which notation to use depends on the specific use case. Dot notation is faster and easier to read, but it cannot be used for accessing properties dynamically or properties with special characters. Bracket notation can be used for accessing properties dynamically and properties with special characters, but it is slightly slower and can be more verbose. As a developer, it’s important to be familiar with both notations and to choose the one that best fits the use case in question.

--

--

Max Headway

Programming nerd just trying to share what I learn with the world.