← JavaScript tutorial

JavaScript

Objects

An object groups related values under named keys: const user = { name: "Ada", age: 21 }.

Example: dot notation vs. bracket notation

const student = {
  name: "Ada",
  track: "Frontend",
  isEnrolled: true,
};

document.getElementById("output").textContent =
  student.name + " is studying " + student["track"];

student.name (dot notation) and student["track"] (bracket notation) both read the same kind of value — the difference is that bracket notation lets the key itself be a variable, computed at runtime: const key = "track"; student[key] works, while student.key would look for a literal property named "key", which doesn't exist. Use dot notation by default; switch to bracket notation only when the property name needs to be dynamic.

Try it yourself