Skip to main content

How to Read a Property of an Object in JavaScript

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A JavaScript object holds values under names, its properties. You read one with a dot and the name, as in book.title, or with the name in square brackets, as in book['title'], which also works when the name is in a variable.

Syntax​

Syntax
<object>.<name>
<object>['<name>']
<object>[<variable>]
PartWhat it does
<object>.<name>Dot notation: the value of the property called <name>.
<object>['<name>']Bracket notation: the same, with the name as a string.
<object>[<variable>]Bracket notation with a variable: the property whose name the variable holds.

MDN's property accessors reference: "Property accessors provide access to an object's properties by using the dot notation or the bracket notation."

The Example​

The example reads a book's title with a dot, its year with brackets, the title again through a variable that holds the name, and an author property the book doesn't have.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which creates a book with title Dune and year 1965, keeps the name title in a variable, then logs book.title, book with year in brackets, book with the variable in brackets and book.author.

main.js
const book = { title: 'Dune', year: 1965 };
const field = 'title';

console.log(book.title);
console.log(book['year']);
console.log(book[field]);
console.log(book.author);

Step 2: Run It​

Terminal
node main.js
Output
Dune
1965
Dune
undefined

A zsh terminal where node main.js prints Dune, 1965, Dune and undefined.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • book.title and book['year'] read the two properties the object has.
  • book[field] used the value of field, the string 'title', as the name.
  • book.author asked for a property the object doesn't have. That isn't an error in JavaScript: the result is undefined.

Common Mistakes​

MistakeWhat happensDo this instead
Reading a property of a missing property, as in book.author.nameTypeError: Cannot read properties of undefined (reading 'name')Use optional chaining: book.author?.name gave undefined
Using a dot with a name that has a hyphen, as in o.first-nameJavaScript reads it as o.first - name: ReferenceError: name is not definedUse brackets: o['first-name']
Writing book.field when the name is in fieldIt looks for a property literally called fieldUse book[field]

Troubleshooting​

SymptomLikely causeFix
undefinedThe object has no property with that name, or it's spelled differentlyCheck the name; they're case-sensitive
TypeError: Cannot read properties of null (reading 'length')The value before the dot is nullCheck for null first, or use ?.

FAQ​

When should I use brackets instead of a dot?​

When the name is in a variable, or isn't a valid identifier, for example when it has a hyphen or a space. Otherwise the dot is shorter.

Can I read a property of a property?​

Yes, by chaining: user.address.city. Use ?. at each step that might be missing.

Conclusion​

object.name and object['name'] read the same property; brackets also take a variable or a name with special characters. A missing property gives undefined, but reading anything from undefined or null throws, which ?. prevents.

Additional Resources​