Objects in JS
Objects in JS
..........................................................
For example:
const person = {
name: 'John',
age: 30,
isStudent: true
};
console.log(person.name);
console.log(person['age'])
........................................
const person = {
name: 'John',
age: 30,
email: '[email protected]',
child: {
address: {
street: '123 Main St',
city: 'Cityville',
state: 'State'
}
},
hobbies: ['reading', 'painting', 'swimming'],
sayHello: function() {
console.log('Hello!');
}
};
Filtering hobbies:
...............................................
Destructuring :
Destructuring Arrays:
// Destructuring array
const [a, b, ...rest] = numbers;
console.log(a); // Output: 1
console.log(b); // Output: 2
console.log(rest); // Output: [3, 4, 5]
...........................................
Destructuring Objects:
const person = {
name: 'John',
age: 30,
city: 'New York'
};
// Destructuring object
const { name, age, city } = person;
..............................................
Array of objects:
console.log(students[1].name);
...........................................................
Array of objects problems:
const books = [
{ title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', pages: 218 },
{ title: 'To Kill a Mockingbird', author: 'Harper Lee', pages: 281 },
{ title: 'Pride and Prejudice', author: 'Jane Austen', pages: 432 },
{ title: '1984', author: 'George Orwell', pages: 328 },
{ title: 'The Catcher in the Rye', author: 'J.D. Salinger', pages: 234 }
];
// Output: [
// { title: 'To Kill a Mockingbird', author: 'Harper Lee', pages: 281 },
// { title: 'Pride and Prejudice', author: 'Jane Austen', pages: 432 },
// { title: '1984', author: 'George Orwell', pages: 328 }
// ]
...........................................................................
const products = [
{ id: 1, name: 'iPhone', price: 999 },
{ id: 2, name: 'Samsung Galaxy', price: 899 },
{ id: 3, name: 'Google Pixel', price: 799 },
{ id: 4, name: 'OnePlus', price: 699 },
{ id: 5, name: 'Xiaomi', price: 499 }
];
// Output: [
// { name: 'iPhone', price: 999 },
// { name: 'Samsung Galaxy', price: 899 },
// { name: 'Google Pixel', price: 799 },
// { name: 'OnePlus', price: 699 },
// { name: 'Xiaomi', price: 499 }
// ]
................................................................
function in javascript:
Functions can be defined using the function keyword, and they can accept input
values called parameters or arguments. Functions can also return a value using the
return statement.
function addNumbers(a, b) {
return a + b;
}
...............................................
Types of Function
Named Functions:
These are functions with a specific name and can be defined using the function
keyword. They can be invoked by using their assigned name.
function greet() {
console.log("Hello!");
}
greet(); // Output: "Hello!"
.......................
Anonymous Functions:
These functions do not have a name and are typically assigned to variables or used
as arguments to other functions.
Arrow functions provide a concise syntax for writing functions. They have a shorter
syntax compared to traditional named or anonymous functions.
single arguments:
...............................................
IIFE (Immediately Invoked Function Expression):
These are self-invoking functions that are executed immediately after they are
defined.
(function() {
console.log("IIFE executed!");
})(); // Output: "IIFE executed!"
//DAY-2
(function(name) {
console.log(`Hello, ${name}! IIFE executed!`);
})("John");
.........................................
Constructor Functions:
Constructor functions are used to create objects. They are typically invoked with
the new keyword to create instances of the objects they define.
function Person(name) {
this.name = name;
}
const john = new Person("John");
console.log(john.name); // Output: "John"
...............................................................
Callback Functions:
Callback functions are passed as arguments to other functions and are invoked at a
later time or when a specific event occurs.
function displayResult(result) {
console.log(`The result is: ${result}`);
}