JavaScript Object
What is an Object in JavaScript?
An object in JavaScript is a collection of key-value pairs, where:
- Keys are called properties
- Values can be any type: string, number, array, function, even another object
Objects are used to store structured data and model real-world entities.
Creating an Object
1. Object Literal Syntax (most common)
let person = {
name: "Alice",
age: 30,
isStudent: false
};
console.log(person); // Output: { name: "Alice", age: 30, isStudent: false }
2. Using new Object()
let user = new Object();
user.name = "Bob";
user.age = 25;
console.log(user); // Output: { name: "Bob", age: 25 }
Accessing Object Properties
1. Dot Notation
console.log(person.name); // Output: Alice
2. Bracket Notation
console.log(person["age"]); // Output: 30
Bracket notation is useful when the property name is dynamic or contains spaces/special characters.
Modifying Object Properties
person.age = 31; // Update
person.country = "India"; // Add new property
delete person.isStudent; // Delete a property
console.log(person);
// Output: { name: "Alice", age: 31, country: "India" }
Object Methods
An object can have functions as values — these are called methods.
let car = {
brand: "Toyota",
start: function () {
return "Car is starting...";
}
};
console.log(car.start()); // Output: Car is starting...
Shorter method syntax (ES6+):
let car = {
brand: "Toyota",
start() {
return "Car is starting...";
}
};
this Keyword in Objects
Inside an object method, this refers to the object itself.
let user = {
name: "Emma",
greet() {
return "Hello, " + this.name;
}
};
console.log(user.greet()); // Output: Hello, Emma
Looping Through Object Properties
Using for...in loop
let student = { name: "Max", grade: "A" };
for (let key in student) {
console.log(key + ": " + student[key]);
}
// Output:
// name: Max
// grade: A
Useful Object Methods
Method | Description |
---|---|
Object.keys(obj) | Returns array of keys |
Object.values(obj) | Returns array of values |
Object.entries(obj) | Returns array of [key, value] pairs |
Object.assign() | Copies properties from one or more objects to another |
Example
let book = { title: "JS Basics", pages: 100 };
console.log(Object.keys(book)); // Output: ["title", "pages"]
console.log(Object.values(book)); // Output: ["JS Basics", 100]
console.log(Object.entries(book));// Output: [["title", "JS Basics"], ["pages", 100]]
Nested Objects
Objects can contain other objects:
let user = {
name: "John",
address: {
city: "New York",
zip: 10001
}
};
console.log(user.address.city); // Output: New York
Objects vs Arrays
Feature | Object | Array |
---|---|---|
Key Type | Strings or Symbols | Indexed (0, 1, 2...) |
Use Case | Named data | Ordered list |
Syntax | { key: value } | [value1, value2] |
Quick Recap
- Objects store key-value pairs.
- Keys are strings (or Symbols), values can be any data type.
- Methods are functions stored in object properties.
- Use this inside methods to refer to the object.
- Use for...in, Object.keys(), Object.values() for accessing properties.
Example Summary
let person = {
name: "Liam",
age: 28,
greet() {
return `Hi, I'm ${this.name}`;
}
};
console.log(person.greet()); // Output: Hi, I'm Liam