Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

6 Objects

1 Hashmaps

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"
};
  • Accessing Values: You can access the value associated with a key using dot notation (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
  • Manipulating Objects: Objects are mutable, meaning their properties can be modified, added, or deleted. You can update values, add new key-value pairs, or remove existing properties using assignment (=) 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)); 
  • Use Cases: Objects are versatile and widely used for organizing and managing data. They are suitable for representing complex data structures, modeling real-world entities, and facilitating data manipulation and retrieval.

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.