# Objects: Properties & Methods

> Master the foundation of JavaScript data structures. Learn how to group related data and functionality together using key-value pairs.

Subject: JavaScript | Lesson 18 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/objects-fundamentals
Previous: [Recursion & the Call Stack](https://codewithanimation.in/javascript/recursion.md) | Next: [References, Copying & structuredClone](https://codewithanimation.in/javascript/object-references-and-copying.md)

## What is an Object?

An object is a collection of related data and/or functionality. Unlike arrays that use numbered indexes, objects use named 'keys' to store 'values'.

Think of an object like a dictionary: you look up a specific word (the key) to find its definition (the value).

## Properties vs Methods

A 'Property' is just a variable attached to an object. For example, `user.name = 'Alice'`.

A 'Method' is a function attached to an object. For example, `user.login()`.

Grouping these together allows you to model real-world concepts perfectly. A `Car` object can have properties like `color` and `speed`, and methods like `drive()`.

## Dot Notation vs Bracket Notation

The most common way to read or write to an object is **Dot Notation**: `user.age = 25`.

However, if the key contains spaces, special characters, or is stored inside a variable, you MUST use **Bracket Notation**: `user['first name'] = 'Alice'` or `user[myVariable] = 25`.

## Common mistake: The undefined trap

If you try to read a property that doesn't exist on an object, JavaScript won't throw an error. Instead, it quietly returns `undefined`. If you then try to read a property *on* that undefined value, your code will crash!

## Tip: Deleting properties

You can completely remove a key-value pair from an object using the `delete` keyword: `delete user.age`. The property will be gone entirely, not just set to undefined.

## Try it: The Object Builder

Use Dot Notation and Bracket Notation to dynamically add, read, and delete properties from the `player` object.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/objects-fundamentals

## Check yourself

### Question 1

When MUST you use bracket notation instead of dot notation?

- A. When the key is stored in a variable or contains a space.
- B. When the value is a number.
- C. When reading a method.

Correct answer: A

Dot notation requires exact, literal, valid identifiers. If your key has a space (`'first name'`) or is dynamic (`const key = 'name'`), bracket notation (`obj[key]`) is mandatory.

- Why not B: Incorrect. The type of the value doesn't matter, only the key name matters.
- Why not C: Incorrect. You usually call methods with dot notation, like `obj.method()`.

### Question 2

What happens if you access `user.height` but the `height` property was never defined?

- A. It returns undefined.
- B. It throws a ReferenceError.
- C. It returns null.

Correct answer: A

Reading a non-existent property on an object gracefully returns `undefined`.

- Why not B: Incorrect. JavaScript is forgiving when reading missing object properties.
- Why not C: Incorrect. `null` is an intentional absence of value. `undefined` means it was never initialized.

### Question 3

How do you permanently remove the `score` property from a `player` object?

- A. delete player.score
- B. player.score = undefined
- C. remove player.score

Correct answer: A

The `delete` operator completely removes the key and its value from the object structure.

- Why not B: Incorrect. The `score` key still exists on the object, its value is just undefined.
- Why not C: Incorrect. `remove` is not a valid JavaScript keyword.

## Remember this

- Objects store data in key-value pairs.
- Properties are variables on an object; Methods are functions on an object.
- Use Dot Notation (`obj.key`) for simple, known keys.
- Use Bracket Notation (`obj['key']`) for spaces, special characters, or dynamic variables.
- The `delete` keyword removes a property entirely.
