An object in JavaScript is a collection of key-value pairs, used to represent a single structured "thing" — a user, a product, a configuration setting. Where an array is good for an ordered list of similar items, an object is good for grouping different pieces of related data together under named keys.
Object Literal Syntax
const user = {
name: "Priya",
age: 28,
isActive: true,
};
console.log(user);
Output:
{ name: 'Priya', age: 28, isActive: true }
Each key: value pair is called a property. Keys are usually strings (quotes are optional if the key is a valid identifier, like name above), and values can be any type — including other objects or arrays, which is how you build nested data structures.
Accessing and Updating Properties
There are two ways to read a property: dot notation and bracket notation.
console.log(user.name); // dot notation — most common
console.log(user["age"]); // bracket notation
const key = "isActive";
console.log(user[key]); // bracket notation lets the key be a variable
user.age = 29; // update an existing property
user.email = "priya@example.com"; // add a new property
console.log(user);
Output:
Priya
28
true
{ name: 'Priya', age: 29, isActive: true, email: 'priya@example.com' }
Dot notation is more common and readable, but bracket notation is required whenever the key is stored in a variable, contains spaces, or isn't known until runtime — exactly like user[key] above.
Deleting Properties and Checking Existence
delete user.email;
console.log("email" in user); // false — the 'in' operator checks for a key
console.log(user.name !== undefined); // true — common existence check
Output:
false
true
Getting All Keys, Values, or Entries
Three built-in Object methods let you turn an object's data into an array, which is especially useful for looping over it with array methods like map() and filter().
const product = { name: "Keyboard", price: 45 };
console.log(Object.keys(product));
console.log(Object.values(product));
console.log(Object.entries(product));
Output:
[ 'name', 'price' ]
[ 'Keyboard', 45 ]
[ [ 'name', 'Keyboard' ], [ 'price', 45 ] ]
Objects are the foundation for working with real-world data in JavaScript — API responses almost always come back as objects or arrays of objects. Once you're comfortable with this, pulling specific properties out of an object becomes much more convenient with object destructuring, and classes show you how to create many objects that share the same shape and behavior.
