Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Properties in Square Brackets

There is another way to read a property: square brackets with the key in quotes.

const stock = {
    apples: 12,
    lemons: 0,
    peaches: 30
};

console.log(stock["peaches"]);
30

stock["peaches"] means the same as stock.peaches; it's just longer. So why use it? Because you can put any value inside the brackets, a variable too. Then you don't need to know the key when you write the program:

const stock = { apples: 12, lemons: 0, peaches: 30 };

const fruit = "apples";
console.log(stock[fruit]);
12

JavaScript first looks into the variable fruit, finds the text "apples" in it, and then reads the property apples. This is most useful in functions, where the key comes as an argument:

const stock = { apples: 12, lemons: 0, peaches: 30 };

function isInStock(fruit) {
    return stock[fruit] > 0;
}

console.log(isInStock("apples"));
console.log(isInStock("lemons"));
true
false

Each call reads a different property, and the function works for any fruit, even one you add to the object later.

Typical mistakes:

  • stock.fruit doesn't look into the variable fruit. It looks for a property whose key is fruit, doesn't find one, and you get undefined. The same happens with stock["fruit"]: in quotes, "fruit" is just text, not the variable.
  • stock[apples] without quotes looks for a variable apples, which doesn't exist: ReferenceError: apples is not defined.

So: when you know the key as you write the code, use a dot. When the key is in a variable, use square brackets, without quotes. Square brackets work for changing values too: stock[fruit] = 5;.

Instructions

  1. 1

    The function printStock should print how many pieces of the given fruit are in stock, but it prints undefined. Fix it so that the first two lines are apples: 12 and peaches: 30.

  2. 2

    Complete the function sell: it should decrease the stock of the given fruit by 1. The program sells two peaches and one apple, so the last two lines should then be peaches: 28 and apples: 11.

Back

4/9

  1. 1. What Is an Object?
  2. 2. Reading Properties
  3. 3. Changing and Adding Properties
  4. 4. Properties in Square Brackets
  5. 5. Arrays of Objects
  6. 6. Looping Through Arrays of Objects
  7. 7. Objects as Arguments
  8. 8. Functions That Create Objects
  9. 9. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.