Skip to content
JavaScript Fundamentals

JavaScript Objects

Beginner Lesson 5 of 10

Objects are collections of key-value pairs that store related data and functionality.

const person = {
firstName: "John",
lastName: "Doe",
age: 30,
fullName() {
return `${this.firstName} ${this.lastName}`;
}
};
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
const john = new Person("John", "Doe");
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
const john = new Person("John", "Doe");
const person = { name: "Alice", age: 25 };
// Dot notation
person.name; // "Alice"
// Bracket notation
person["age"]; // 25
// Destructuring
const { name, age } = person;
const obj = { a: 1, b: 2, c: 3 };
Object.keys(obj); // ["a", "b", "c"]
Object.values(obj); // [1, 2, 3]
Object.entries(obj); // [["a", 1], ["b", 2], ["c", 3]]
// Spread operator
const newObj = { ...obj, d: 4 };
// Object.assign
const merged = Object.assign({}, obj, { d: 4 });
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function() {
console.log(`${this.name} makes a sound`);
};
const dog = new Animal("Dog");
dog.speak(); // "Dog makes a sound"

Continue to Arrays →