Object
1. What is an Object?
Objects in JavaScript can be compared to game characters. User characters have the same job and abilities, but each has different detailed content.
Someone has an ID of Ella and a job as a wizard, while someone else has an ID of Chloe and a job as a warrior.
Similarly, when users register on a website, the items they need to enter are all the same, but the information they enter is different for each user. In cases like this where each can have different values but the types of data that need to be entered are the same, using objects makes it easy to manage data.
In such cases where there are common properties, you should use objects. Let's assume you're creating an address book of members who registered on a website.
// Do you need to declare multiple variables like this every time? let userFirstName = 'Ella'; let userLastName = 'Choi'; let userEmail = 'ella@gmail.com'; let userCity = 'Seoul'; let userFirstName = 'Chloe'; let userLastName = 'Kim'; let userEmail = 'chloe@gmail.com'; let userCity = 'Seoul';let user = { // create object with curly braces firstName: 'Ella', // separate pairs of names and values with ',', separate names and values with ':' lastName: 'Choi', // here lastName is the key, 'Choi' is the value email: 'ella@gmail.com', city: 'Seoul', };
2. Two Ways to Access Object Values
Dot notation: object + .(dot) + key value
Bracket notation: key value as 'string' inside square brackets
3. Various Ways to Handle Objects
Bracket notation: must be used when key values change dynamically
You can also add values using dot/bracket notation
You can also delete properties using the delete keyword
You can check if a key exists using the in operator
4. Common Mistakes When Writing Strings
To prevent minor mistakes like forgetting square brackets when using bracket notation, or not declaring variables, etc., let's understand and familiarize ourselves with the content below one by one!
Last updated