Javascript - Object Object properties

Introduction

Object properties are typically accessed using dot notation.

It's possible to access properties via bracket notation.

console.log(person["name"]);    //"First"
console.log(person.name);       //"First"

Bracket notation allows you to use variables for property access:

var propertyName = "name";
console.log(person[propertyName]);    //"First"

You can use bracket notation for the property name with space characters. For example:

person["first name"] = "First";

Since the name "first name" contains a space, you can't use dot notation to access it.

Related Topics