| Javascript Array Methods | Javascript-DOM-Manipulation | |
Objects and Object Destructuring and Spread/Rest Operators in JavaScript |
An object is a collection of keyโvalue pairs. Keys are called properties, and values can be any data type (string, number, array, function, etc.).
Properties are variables attached to an object.
let person = {
name: "Alice",
age: 25,
city: "Hyderabad"
};
console.log(person.name); // Alice
console.log(person["age"]); // 25
A method is a function stored as a property of an object. Methods define the behavior of the object.
let person = {
name: "Alice",
age: 25,
greet: function() {
return "Hello, my name is " + this.name;
}
};
console.log(person.greet()); // Hello, my name is Alice
this Keyword
The this keyword refers to the object that is executing the current function.
Inside a method, this refers to the object itself.
let car = {
brand: "Toyota",
model: "Corolla",
start: function() {
console.log(this.brand + " " + this.model + " is starting...");
}
};
car.start(); // Toyota Corolla is starting...
let user = { name: "Bob" };
// Add
user.age = 30;
// Update
user.name = "Robert";
// Delete
delete user.age;
console.log(user); // { name: "Robert" }
let calculator = {
add: function(a, b) {
return a + b;
},
subtract: function(a, b) {
return a - b;
},
multiply: function(a, b) {
return a * b;
},
divide: function(a, b) {
return b !== 0 ? a / b : "Cannot divide by zero";
}
};
console.log(calculator.add(10, 5)); // 15
console.log(calculator.subtract(10, 5)); // 5
console.log(calculator.multiply(10, 5)); // 50
console.log(calculator.divide(10, 5)); // 2
Destructuring allows you to extract values from objects (or arrays) into variables in a clean, concise way.
// Example object
const user = { name: "Alice", age: 25, city: "Hyderabad" };
// Destructuring
const { name, age } = user;
console.log(name); // Alice
console.log(age); // 25
const { name: userName, city: location } = user;
console.log(userName); // Alice
console.log(location); // Hyderabad
const { country = "India" } = user;
console.log(country); // India (default used)
const colors = ["red", "green", "blue"]; const [first, second] = colors; console.log(first); // red console.log(second); // green
The spread operator expands elements of an array or properties of an object.
// Arrays
const arr1 = [1, 2, 3];
const arr2 = [4, 5];
const combined = [...arr1, ...arr2];
console.log(combined); // [1, 2, 3, 4, 5]
// Objects
const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3 };
const merged = { ...obj1, ...obj2 };
console.log(merged); // { a:1, b:2, c:3 }
The rest operator collects remaining elements into an array or object.
// Arrays
const [x, y, ...rest] = [10, 20, 30, 40, 50];
console.log(x); // 10
console.log(y); // 20
console.log(rest); // [30, 40, 50]
// Objects
const { name: n, ...others } = user;
console.log(n); // Alice
console.log(others); // { age: 25, city: "Hyderabad" }
const basicInfo = { name: "Alice", age: 25 };
const contactInfo = { email: "alice@example.com", city: "Hyderabad" };
// Merge using spread
const userProfile = { ...basicInfo, ...contactInfo };
console.log(userProfile);
// { name: "Alice", age: 25, email: "alice@example.com", city: "Hyderabad" }
// Destructure
const { name, email, city } = userProfile;
console.log(name, email, city);
// Alice alice@example.com Hyderabad
| Javascript Array Methods | Javascript-DOM-Manipulation | |