Objects in JavaScript
An object in JavaScript is a collection of related data and behaviour stored as key-value pairs, called properties. Where an array holds an ordered list, an object describes one thing with named parts: a user with a name, email and age, or a product with a title, price and stock count. Objects are everywhere in JavaScript; arrays, functions, dates and DOM elements are all objects underneath.
Creating an object literal
Curly braces create an object. Each property has a key, a colon and a value, and properties are separated by commas. Values can be anything, including arrays, other objects and functions. A function stored on an object is called a method.
const student = {
name: 'Kabir',
age: 21,
skills: ['HTML', 'CSS'],
address: { city: 'Lucknow', pin: '226001' },
'favourite subject': 'Maths', // key with a space needs quotes
introduce() {
return `Hi, I am ${this.name}`;
},
};
console.log(student.name); // dot notation
console.log(student['favourite subject']); // bracket notation
console.log(student.address.city); // nested
console.log(student.introduce()); // method call
student.email = 'kabir@example.com'; // add
student.age = 22; // update
delete student.skills; // remove
const key = 'age';
console.log(student[key]); // 22: dynamic key
console.log(student.phone); // undefined, not an error
Dot versus bracket notation
Dot notation is shorter and used most of the time. Bracket notation is needed when the key contains spaces or hyphens, starts with a number, or is stored in a variable. A common real use is looking up a value based on user choice, for example prices[selectedSize].
Reading a property that does not exist returns undefined rather than throwing. But reading a property of undefined does throw. Optional chaining, student.address?.city, safely returns undefined if address is missing.
Handy shortcuts and helpers
- Shorthand properties: if a variable has the same name as the key, write
{ name, age }instead of{ name: name, age: age }. - Destructuring:
const { name, age } = student;pulls properties into variables. Object.keys(),Object.values(),Object.entries(): give arrays of keys, values or key-value pairs, which you can loop over.'email' in studentorObject.hasOwn(student, 'email'): check whether a property exists.
Objects are shared by reference
This catches nearly every beginner. When you assign an object to another variable, you do not copy it. Both variables point to the same object in memory.
So after const copy = student; copy.name = 'Riya';, student.name is also 'Riya'. The same happens when you pass an object to a function: changes inside the function affect the original. Comparing two separate objects with === is false even if they have identical contents, because they are different objects.
To make a copy, use spread syntax: const copy = { ...student };. This is a shallow copy. Top-level properties are copied, but nested objects such as address are still shared. For a full deep copy of plain data, use structuredClone(student), which also handles nested arrays and objects. Note that const only stops you reassigning the variable; it does not stop properties from being changed.
Takeaway
Objects group named values describing one thing. Use dot notation by default and brackets for dynamic or unusual keys, loop with Object.entries(), and remember that assigning an object shares it rather than copying it. The following lessons go deeper into creating objects and adding methods.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.