Skip to content
UniCircle
EasyConceptualObjects

Dot vs bracket notation and checking properties

Question JS-OBJ-003

When must you use bracket notation instead of dot notation? What are the different ways to check whether an object has a property?

Show the explanation

Model answer. Dot notation (user.name) works when the key is a valid identifier written in code. Use bracket notation (user[key]) when:

  • the key is in a variable or computed: scores[subject]
  • the key has spaces, hyphens or starts with a digit: headers['content-type']
  • the key is a symbol

Checking for a property:

JavaScript
const p = { name: 'Ravi', age: undefined };
'age' in p;                 // true (also true for inherited keys like 'toString')
Object.hasOwn(p, 'age');    // true, own keys only (modern, preferred)
p.age !== undefined;        // false! the key exists but holds undefined
p.address?.city;            // safe read of a possibly missing nested key

Use Object.hasOwn (or the older hasOwnProperty) when you need "own key exists", and in when inherited keys count too. Comparing with undefined cannot tell "missing" from "set to undefined".

Original UniCircle question. Last reviewed 22 Oct 2026.