How many ways can a property of a JavaScript object be accessed?

JavaScript object properties can be accessed in two main ways: dot notation and bracket notation. Each method has its own use cases and advantages.

Dot Notation

The dot notation is the most common and readable way to access object properties when the property name is known at compile time.

object.propertyName

Bracket Notation

The bracket notation uses square brackets with the property name as a string. This method is more flexible and allows dynamic property access.

object["propertyName"]
object[variableName]

Example: Using Dot Notation

<html>
<body>
<script>
var person = {
    firstname: "Ram",
    lastname: "Kumar",
    age: 50,
    designation: "content developer"
};
document.write(person.firstname + " is in a role of " + person.designation);
</script>
</body>
</html>
Ram is in a role of content developer

Example: Using Bracket Notation

<html>
<body>
<script>
var person = {
    firstname: "Ram",
    lastname: "Kumar",
    age: 50,
    designation: "content developer"
};
document.write(person["firstname"] + " is in a role of " + person["designation"]);
</script>
</body>
</html>
Ram is in a role of content developer

Example: Dynamic Property Access

<html>
<body>
<script>
var person = {
    firstname: "Ram",
    lastname: "Kumar",
    age: 50
};

var propertyName = "firstname";
document.write("Dynamic access: " + person[propertyName]);
</script>
</body>
</html>
Dynamic access: Ram

Comparison

Method When to Use Advantages
Dot notation Known property names Clean, readable syntax
Bracket notation Dynamic properties, special characters Flexible, allows variables as keys

Key Points

  • Use dot notation for static property names that are valid JavaScript identifiers
  • Use bracket notation when property names contain spaces, special characters, or are stored in variables
  • Bracket notation is required for computed property names
  • Both methods return undefined for non-existent properties

Conclusion

Both dot and bracket notation serve different purposes in JavaScript. Choose dot notation for simplicity and bracket notation for dynamic property access or special property names.

Updated on: 2026-03-15T23:18:59+05:30

422 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements