Learn Programming Fundamentals
1 Programming Fundamentals
2 Variables Data Types
3 Conditional Structures
4 Strings
5 Arrays
8 Functions
When dealing with data that comprises different attributes, like a person's information such as their name, age, and city, using multiple variables can become cumbersome. Even arrays cannot help here because arrays can hold the same type of data, while name
is a string and age
is a number. That's where objects come into play. Objects, also known as hash maps or dictionaries in other programming languages, provide a way to organize data using key-value pairs.
Here's an overview of the main points about objects:
Key-Value Pairs: Objects store data in key-value pairs. Each entry consists of a key, which acts as an identifier, and its corresponding value.
Keys: Keys can be strings, numbers, or symbols. They serve as unique identifiers within the object.
Values: Values can be of any data type, including strings, numbers, arrays, or even other objects. They represent the actual data associated with each key.
Let's take a look at a JavaScript example that creates a person
object holding the name, age, and city:
let person = { name: "John", age: 30, city: "New York" };
objectName.key
) or bracket notation (objectName['key']
).let person = { name: "John", age: 30, city: "New York" }; console.log(person.name); // Output: John console.log(person['age']); // Output: 30
=
) or object manipulation methods.let person = { name: "John", age: 30, city: "New York" }; person.age = 31; // Update age person.country = "USA"; // Add new property delete person.city; // Remove city property console.log(JSON.stringify(person));
In summary, objects provide an efficient and flexible way to store and access data through key-value pairs, making them indispensable for various programming tasks and data management operations.