Back to JavaScript
2026-03-065 min read

Property accessors (JavaScript)

Learn Property accessors (JavaScript) step by step with clear examples and exercises.

Why This Matters

JavaScript property accessors are a fundamental aspect of the language that enables developers to interact dynamically with an object's properties using both dot notation and bracket notation. Understanding property accessors is crucial for solving complex problems, writing efficient code, and preparing for interviews or real-world programming challenges. This article will delve into the world of property accessors, their significance in real-world scenarios, and common mistakes to avoid when working with them.

In this article, we will cover:

  1. Understanding Properties
  2. Dot Notation
  3. Bracket Notation
  4. Accessing Properties with Special Characters
  5. Accessing Properties Dynamically
  6. Worked Example
  7. Common Mistakes
  8. Practice Questions
  9. FAQ

Prerequisites

To fully grasp the concept of JavaScript property accessors, you should have a solid understanding of:

  1. Variables and data types in JavaScript
  2. Objects and their properties
  3. Basic JavaScript syntax (e.g., functions, loops)
  4. Understanding the difference between arrays and objects
  5. Knowledge about JavaScript's special characters and string manipulation
  6. Familiarity with conditional statements and error handling

Core Concept

Understanding Properties

In JavaScript, an object can be thought of as a collection of key-value pairs, where keys are strings or symbols, and values are any data type. For example:

const person = {
firstName: "John",
lastName: "Doe"
};

In this example, person is an object with two properties: firstName and lastName.

Dot Notation

To access or modify a property using dot notation, you simply append the property name to the object followed by a dot (.). For instance:

console.log(person.firstName); // Outputs: "John"
person.lastName = "Smith";
console.log(person.lastName); // Outputs: "Smith"

Bracket Notation

Alternatively, you can access or modify a property using bracket notation by enclosing the property name in square brackets ([]). This is particularly useful when property names are variables or dynamic values. For example:

const propertyName = "lastName";
console.log(person[propertyName]); // Outputs: "Doe"
person["firstName"] = "Jane";
console.log(person.firstName); // Outputs: "Jane"

Accessing Properties with Special Characters

When a property name contains special characters, such as dashes or underscores, you can use bracket notation to access it correctly. For instance:

const obj = {
"first-name": "John",
"last-name": "Doe"
};
console.log(obj["first-name"]); // Outputs: "John"

Accessing Properties Dynamically

You can access properties dynamically using bracket notation with a variable containing the property name:

const propertyName = "lastName";
console.log(person[propertyName]); // Outputs: "Doe"

Worked Example

Let's create a simple example where we dynamically access and modify object properties using both dot and bracket notation.

const person = {
firstName: "John",
lastName: "Doe"
};

// Accessing properties with dot notation
console.log(person.firstName); // Outputs: "John"
console.log(person.lastName); // Outputs: "Doe"

// Modifying properties with dot notation
person.age = 30;
console.log(person.age); // Outputs: 30

// Accessing properties with bracket notation
const propertyName = "firstName";
console.log(person[propertyName]); // Outputs: "John"

// Modifying properties with bracket notation
person["lastName"] = "Smith";
console.log(person.lastName); // Outputs: "Smith"

Common Mistakes

  1. Forgetting to include the dot or square brackets when accessing a property: This will result in an error if the property name is not found as a variable in the current scope.
  1. Accessing non-existent properties: If you attempt to access a property that does not exist, JavaScript will return undefined. To avoid this, always check for property existence before accessing it or handle the undefined case gracefully.
  1. Using dot notation with special characters in property names: While bracket notation can be used to access properties with special characters, dot notation cannot.
  1. Confusing object properties with array indices: Remember that dot and square bracket notation have different purposes: dot notation is for accessing object properties, while square bracket notation is for accessing array elements by index.
  1. Using dot notation with numeric property names: Dot notation can only be used to access string-named properties. If you need to access a property with a numeric name, use bracket notation instead.
  1. Not handling errors when accessing non-existent properties: Failing to handle errors when accessing non-existent properties can lead to unexpected behavior in your code. Always check for property existence before accessing it or handle the undefined case gracefully.
  1. Incorrectly using both dot and bracket notation together: You should use either dot or bracket notation for accessing properties, but not both together.

Practice Questions

  1. Given the following object, use both dot and bracket notation to access the value of city:
const address = {
street: "Main St",
city: "New York"
};
  1. Write a function that accepts an object and a property name as arguments, then returns the property value using both dot and bracket notation.
  1. Modify the following code to use bracket notation instead of dot notation for accessing the age property:
const person = {
firstName: "John",
lastName: "Doe",
age: 30
};
console.log(person.age); // Outputs: 30
  1. Write a function that takes an object and returns a new object with the same properties but with all property names converted to camelCase. For example, given the following input:
const obj = {
"first-name": "John",
"last-name": "Doe"
};

The function should return:

{ firstName: "John", lastName: "Doe" }

FAQ

  1. What happens if I try to access a non-existent property using dot notation? JavaScript will return undefined.
  1. Can I use bracket notation with dot notation to access properties in JavaScript? No, you should use either dot or bracket notation for accessing properties, but not both together.
  1. How can I check if a property exists before accessing it using dot notation? You can use the hasOwnProperty() method to check if an object has a specific property:
if (person.hasOwnProperty("age")) {
console.log(person.age); // Outputs: age value, if it exists
} else {
console.log("Property not found");
}
  1. Can I use dot notation to access properties with special characters in their names? No, you should use bracket notation for properties with special characters.
  1. How can I check if an object has a specific property using bracket notation? You can use the in operator to check if an object has a specific property:
if ("age" in person) {
console.log(person["age"]); // Outputs: age value, if it exists
} else {
console.log("Property not found");
}
  1. How can I access an array element using dot notation? Dot notation is used for object properties, not array elements. To access an array element, use square bracket notation with the index:
const arr = [1, 2, 3];
console.log(arr[0]); // Outputs: 1
  1. How can I handle errors when accessing non-existent properties using bracket notation? You can use try/catch blocks to handle errors when accessing non-existent properties using bracket notation:
try {
const propertyValue = person[propertyName];
console.log(propertyValue);
} catch (error) {
console.log("Property not found");
}
Property accessors (JavaScript) | JavaScript | XQA Learn