Script Valley
JavaScript: The Complete Language
Objects, Arrays, and DestructuringLesson 3.1

JavaScript objects: creating, accessing, and mutating

object literals, dot notation, bracket notation, computed property names, property shorthand, Object.keys Object.values Object.entries, property existence check, delete operator

Objects Are Key-Value Maps

An object is an unordered collection of key-value pairs called properties. Keys are strings (or Symbols); values can be any type including other objects or functions.

Creating and Accessing

const user = {
  name: "Ana",
  age: 28,
  "is-admin": false // keys with hyphens need quotes
};

user.name          // "Ana"   — dot notation
user["is-admin"]   // false   — bracket notation (required for special chars)

const key = "age";
user[key]          // 28 — dynamic property access

Property Shorthand and Computed Keys

const name = "Bob";
const role = "admin";

// shorthand: variable name becomes key name
const user2 = { name, role }; // { name: "Bob", role: "admin" }

// computed property names
const prefix = "get";
const api = { [`${prefix}User`]: () => user };

Inspecting and Modifying

Object.keys(user)    // ["name", "age", "is-admin"]
Object.values(user)  // ["Ana", 28, false]
Object.entries(user) // [["name","Ana"],["age",28],["is-admin",false]]

"name" in user  // true — safer than user.name !== undefined
delete user.age // removes the property

Objects are passed by reference. When you assign an object to a new variable, both variables point to the same object in memory — modifying one changes the other.

Up next

Arrays: methods every JavaScript developer must know

Sign in to track progress