In JavaScript, an object is a discrete entity characterized by properties and a specific type. Objects are utilized to manage substantial amounts of data within extensive applications.
For instance, envision a book as an object with properties like its title, author's name, and the number of pages it contains.
To declare an object in JavaScript, you can use the following syntax:
const book = {"Title": "A Passage to India", "Pages": "500", "Author": "E.M. Foster"};
Accessing properties of JavaScript objects can be achieved through three different methods:
- Dot Property Accessor
- Example: console.log(book.Title); // => A Passage to India
- Alternatively, you can use identifiers within square brackets:
console.log(book['Author']); // => 'E.M. Foster
- Square Brackets Property Accessor
You can also use square brackets to access properties. For example:
const property = 'Title';
const book = {
title: 'A Passage to India'
};
// Square brackets property accessor
console.log(book['Title']); // => 'A Passage to India'
console.log(book[property]); // => 'A Passage to India' -
Object Destructuring: Object destructuring allows you to extract properties into variables. Here's an example:
const book = {
title: 'A Passage to India'
};
// Object destructuring
const { title } = book;
console.log(title); // => 'A Passage to India'
These methods provide various ways to access and manipulate object properties in JavaScript.
Post a Comment