devnotes.

Creating and Using Objects

Objects store data as key-value pairs and are the foundation of almost everything in JavaScript.

const user = {
  name: "Fatima",
  age: 28,
  greet() {
    return `Hi, I'm ${this.name}`;
  },
};

console.log(user.name);     // dot notation
console.log(user["age"]);   // bracket notation

Dot notation (user.name) is the common, readable form, but bracket notation (user["age"]) is required whenever the key is dynamic — stored in a variable — or isn't a valid identifier, e.g. user["favorite color"].

Method shorthand (greet() {...}) inside an object literal is equivalent to writing greet: function() {...}, just shorter. Inside a regular method like this, this refers to the object the method was called on.