본문으로 건너뛰기

Objects

Objects

An object is a collection of data consisting of pairs of keys and values.

Here, the key is a string, and the value can be of various data types (primitives, objects, functions, etc.).


Creating an Object

In JavaScript, objects are created using curly braces { }.

Your wallet, for instance, can be represented as an object like this:

let wallet = {
cash: 10000,
creditCard: 'Visa',
photo: 'family photo',
driverLicense: true,
};

Within the object, things like cash, creditCard, photo, driverLicense are called 'properties' or 'keys', and the associated pieces of data are called 'values'.


Retrieving Information from an Object

  1. Using dot notation (.): This is the most common method used.

    let myMoney = wallet.cash; // 10000

  1. Using bracket notation ([]) with keys: This is useful when the key is provided as a variable, or when it contains spaces or special characters.

    let card = 'creditCard';
    let myCard = wallet[card]; // 'Visa'

Modifying and Adding Object Information

You can modify the information in an object or add new information using either dot (.) or bracket ([]) notation.

// Adding a new property
wallet.height = 170;
wallet['creditCard'] = 'MasterCard';

// Changing the value of an existing property
wallet.cash = 5000;

다음 내용이 궁금하다면?

월 12,500원 PLUS 멤버십 가입 or 강의를 등록해 주세요!