JavaScript (JS) objects are one of the core data structures used to store and manage data.
They represent collections of key-value pairs, where:
Keys are strings (or Symbols).
Values can be any type: string, number, function, array, another object, etc.
Eg:-
const person =
{ name: "Alice",
age: 30,
isEmployed: true,
greet: function() {
console.log("Hell
o!");
};
Accessing Properties
console.log(person.name); // Dot notation → "Alice"
console.log(person*"age"+); // Bracket notation → 30
Bracket notation is useful for dynamic property access:
const key = "isEmployed";
console.log(person[key]); // true
◻ Modifying & Adding Properties
person.age = 31; // Modify existing
person.city = "New York"; // Add new
◻ Deleting Properties
delete person.isEmployed;
◻ Nested Objects
const company =
{ name: "Tech
Corp",
address: {
city: "San Francisco",
zip: "94107"
}
};
console.log(company.addres
s.city); // "San
Francisco"
◻ Looping Through
Properties
for (let key in person)
{ console.log(key + ": " +
person[key]);
}
◻ Object Methods
Built-in methods to interact with objects:
Object.keys(person); // ["name", "age", "greet"]
Object.values(person); // ["Alice", 30, function]
Object.entries(person); // [["name", "Alice"], ["age", 30], ["greet",
function]]
◻ Object Destructuring
const { name, age } = person;
console.log(name, age); // "Alice", 30
1. Methods in JavaScript
A method is just a function that is a property of an object.
◻ Example:
const car =
{ brand:
"Toyota",
start: function()
{
console.log("Eng
ine
started.");
},
drive()
{ console.log("Driving..."
);
}
};
car.start(); // Engine started.
car.drive(); // Driving...
start and drive are both methods of
the car object.
2. Constructor Functions
A constructor is a function used to create multiple instances (objects) with the same
structure and behavior.
◻ Basic Constructor Function:
function Person(name, age) {
this.name = name;
this.age = age;
this.greet = function()
{ console.log(`Hi, I'm $
{this.name}`);
};
}
const person1 = new Person("Alice", 30);
const person2 = new Person("Bob", 25);
person1.greet(); // Hi, I'm Alice
person2.greet(); // Hi, I'm Bob
new keyword creates a new object, sets
this to it, and returns it.
◻ ◻ Avoiding Duplication with Prototypes
Putting methods on the prototype is more memory-efficient:
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.greet = function()
{ console.log(`Hi, I'm $
{this.name}`);
};
const p1 = new Person("Charlie", 40);
p1.greet(); // Hi, I'm Charlie
JavaScript Object Properties
There are two types of properties in JavaScript:
1. Data properties
2. Accessor properties (getters/setters)
And then there's the prototype, which is a core part of how objects inherit properties and
methods.
1. Data Properties
These are the most common type — they hold a static value.
◻ Example:
const person = {
name: "Alice",
age: 30
};
console.log(pers
on.name); //
"Alice"
Here, name and age
are data
properties.
Each data property
has 4 attributes
(invisible unless
you inspect them):
value: The
actual value
("Alice",
30)
writable:
If the value
can be
changed
enumerable
: If it shows
up in loops
like
for...in
configurab
le: If the
property can
be deleted
or changed
You can define
them manually:
Object.defineProperty(person, "gender", {
value: "female",
writable: true,
enumerable: true,
configurable: true
});
2. Accessor
user.fullName = "Jane Smith";
console.log(user.firstName); // "Jane"
console.log(user.lastName); // "Smith"
Key Difference:
Data property → stores value
Accessor property → calculates or sets
value using a function
You can also define them using
Object.defineProperty:
Object.defineProperty(user, "fullName", {
get: function() {
return `${this.firstName} $
{this.lastName}`;
},
set: function(value) {
[this.firstName, this.lastName] =
value.split(" ");
}
}
)
;
3.
Pr
ot
oty
pe
Eve
ry
Jav
aSc
ript
obj
ect
has
an
inte
rnal
lin
k to
ano
ther
obj
ect
call
ed
its
pro
tot