JavaScript Objects - Complete Guide
Objects are one of the most important concepts in JavaScript. They let you store and organize data using key-value pairs. Understanding objects is essential for JavaScript development!
What is an Object?
An object is a collection of properties. Each property has a name (key) and a value. Think of it like a dictionary or a person's profile card.
let person = {
name: "John",
age: 25,
city: "New York"
};Creating Objects
Object Literal (Most Common)
let person = {
name: "John",
age: 25,
city: "New York"
};Object Constructor
let person = new Object();
person.name = "John";
person.age = 25;
person.city = "New York";Empty Object
let empty = {};
let empty2 = new Object();Accessing Object Properties
Dot Notation
let person = {
name: "John",
age: 25
};
console.log(person.name); // "John"
console.log(person.age); // 25Bracket Notation
let person = {
name: "John",
age: 25
};
console.log(person["name"]); // "John"
console.log(person["age"]); // 25
// Useful for dynamic property access
let prop = "name";
console.log(person[prop]); // "John"Modifying Objects
Adding Properties
let person = {
name: "John"
};
person.age = 25; // Add property
person["city"] = "New York"; // Add propertyUpdating Properties
let person = {
name: "John",
age: 25
};
person.age = 30; // Update propertyDeleting Properties
let person = {
name: "John",
age: 25
};
delete person.age; // Remove propertyObject Methods
Objects can contain functions (called methods):
let person = {
name: "John",
age: 25,
greet: function() {
return `Hello, I'm ${this.name}`;
}
};
console.log(person.greet()); // "Hello, I'm John"Shorthand Method Syntax
let person = {
name: "John",
age: 25,
greet() {
return `Hello, I'm ${this.name}`;
}
};The 'this' Keyword
this refers to the current object:
let person = {
name: "John",
age: 25,
introduce() {
return `Hi, I'm ${this.name} and I'm ${this.age} years old`;
}
};
console.log(person.introduce());Nested Objects
Objects can contain other objects:
let person = {
name: "John",
age: 25,
address: {
street: "123 Main St",
city: "New York",
zip: "10001"
}
};
console.log(person.address.city); // "New York"Object Methods
Object.keys() - Get All Keys
let person = {
name: "John",
age: 25,
city: "New York"
};
let keys = Object.keys(person);
console.log(keys); // ["name", "age", "city"]Object.values() - Get All Values
let person = {
name: "John",
age: 25,
city: "New York"
};
let values = Object.values(person);
console.log(values); // ["John", 25, "New York"]Object.entries() - Get Key-Value Pairs
let person = {
name: "John",
age: 25
};
let entries = Object.entries(person);
console.log(entries); // [["name", "John"], ["age", 25]]Object.assign() - Copy Object
let person = {
name: "John",
age: 25
};
let copy = Object.assign({}, person);Spread Operator (Modern Way)
let person = {
name: "John",
age: 25
};
let copy = { ...person };
let updated = { ...person, age: 30 };Looping Through Objects
for...in Loop
let person = {
name: "John",
age: 25,
city: "New York"
};
for (let key in person) {
console.log(key + ": " + person[key]);
}Object.keys() with forEach
let person = {
name: "John",
age: 25,
city: "New York"
};
Object.keys(person).forEach(key => {
console.log(key + ": " + person[key]);
});Object Destructuring
Extract properties into variables:
let person = {
name: "John",
age: 25,
city: "New York"
};
// Destructuring
let { name, age } = person;
console.log(name); // "John"
console.log(age); // 25
// With different variable names
let { name: personName, age: personAge } = person;Classes (ES6)
Create objects using classes:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hello, I'm ${this.name}`;
}
}
let person = new Person("John", 25);
console.log(person.greet());Visual Explanation: Object Structure
Here's how objects are structured:
Object Structure:
┌─────────────────────────────┐
│ person = { │
│ name: "John", │
│ age: 25, │
│ city: "New York" │
│ } │
│ │
│ Keys: name age city │
│ Values: "John" 25 "NY" │
└─────────────────────────────┘Frequently Asked Questions (FAQ)
Q1: What's the difference between objects and arrays?
A:
- Objects - Key-value pairs, accessed by key
person.name - Arrays - Ordered list, accessed by index
arr[0]
Use objects for named properties, arrays for ordered lists.
Q2: When should I use dot notation vs bracket notation?
A:
- Dot notation - When you know the property name
person.name - Bracket notation - When property name is dynamic
person[propName]
Q3: Can I use variables as object keys?
A: Yes! Use bracket notation:
let key = "name";
let person = { [key]: "John" }; // { name: "John" }Q4: What's the difference between Object.assign and spread operator?
A: Both copy objects, but spread is more modern:
let copy1 = Object.assign({}, obj);
let copy2 = { ...obj }; // Modern wayQ5: How do I check if a property exists?
A: Use in operator or hasOwnProperty:
if ("name" in person) { }
if (person.hasOwnProperty("name")) { }Q6: Can objects have functions?
A: Yes! Functions in objects are called methods:
let obj = {
greet() {
return "Hello!";
}
};Q7: What's object destructuring?
A: Extracting properties into variables:
let { name, age } = person;
// Same as:
let name = person.name;
let age = person.age;Q8: Can I add methods to existing objects?
A: Yes!
let person = { name: "John" };
person.greet = function() {
return "Hello!";
};Q9: What's the difference between shallow and deep copy?
A:
- Shallow copy - Copies object, but nested objects are references
- Deep copy - Completely independent copy
Use JSON.parse(JSON.stringify(obj)) for deep copy (simple objects only).
Q10: Can I use objects as keys?
A: No! Object keys must be strings or symbols. Objects are converted to strings [object Object] when used as keys.
Next Steps
Now that you understand objects, here's what to learn next:
- Next: Learn about JavaScript DOM - Interact with web pages
- Explore JavaScript Events - Handle user interactions
- Understand JavaScript Async - Promises and async/await
- Master JavaScript ES6+ - Modern JavaScript features
Objects are fundamental in JavaScript. Practice creating and manipulating objects to get comfortable!