JavaScript Property accessors

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

Read or write object properties with obj.key (dot) or obj["key"] (brackets). Brackets allow computed names and keys that are not valid identifiers.

JavaScript Syntax

JavaScript
const user = { name: "Ada", "job-title": "Engineer" };
console.log(user.name);
console.log(user["job-title"]);
const k = "name";
console.log(user[k]);
Try it Editor

How It Works

  1. Dot. For valid identifiers.
  2. Brackets. For odd keys or variables.

Examples

Example 1 — Assignment

Write properties.

JavaScript
const o = {};
o.a = 1;
o["b"] = 2;
console.log(o.a, o.b);
Try it Editor

How It Works

  1. Both forms write properties.

Example 2 — Missing property

Returns undefined.

JavaScript
console.log({}.nope);
Try it Editor

How It Works

  1. No throw for missing keys on normal objects.

Example 3 — Optional chaining

Safe deep reads.

JavaScript
console.log({}.a?.b);
Try it Editor

How It Works

  1. ?. short-circuits on nullish.

Frequently Asked Questions

When the key is in a variable, or is not a valid identifier (spaces, dashes).
Arrays use numeric indexes, but any object can have a "0" key.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful