JavaScript Objects
1. What is an Object?
An object in JavaScript is a collection of key-value pairs where the keys
(properties) are strings (or Symbols), and the values can be any data type.
let person = {
name: "John",
age: 30,
isEmployed: true
};
2. Creating Objects
Using Object Literals (Most Common)
let car = {
brand: "Toyota",
model: "Camry",
year: 2022
};
Using the new Object() Constructor
let user = new Object();
[Link] = "Alice";
[Link] = 25;
Using [Link]() (Prototype-Based)
let parent = {
greet: function() {
[Link]("Hello!");
}
};
let child = [Link](parent);
[Link] = "Kid";
[Link]([Link]); // Kid
[Link](); // Hello!
3. Accessing and Modifying Properties
Dot Notation (Recommended)
[Link]([Link]); // John
[Link] = 31;
Bracket Notation (Useful for dynamic keys)
[Link](person["name"]); // John
let key = "age";
[Link](person[key]); // 31
Adding and Deleting Properties
[Link] = "New York"; // Add new property
delete [Link]; // Remove a property
4. Object Methods (Functions inside Objects)
let student = {
name: "Emma",
greet: function() {
[Link](`Hello, my name is ${[Link]}`);
}
};
[Link](); // Hello, my name is Emma
📝 this refers to the current object.