How do we access object properties?

QuestionsQuestions4 SkillsProDec, 23 2025
0188

Great question! Once you have data stored in an object, there are two primary ways to access it in JavaScript: Dot Notation and Bracket Notation.

Let's use the object you've been working on in your script.js as an example:

const car = {
  Ford: "explorer",
  Tesla: "Model 3"
};

1. Dot Notation (Most Common)

You use a period (.) followed by the property name. It is clean and easy to read.

console.log(car.Ford); // Output: "explorer"

2. Bracket Notation

You use square brackets [] and the property name as a string inside them.

console.log(car["Tesla"]); // Output: "Model 3"

Which one should you use?

  • Use Dot Notation 90% of the time because it's faster to type and easier to read.
  • Use Bracket Notation if:
    1. The property name has a space in it (e.g., car["fuel type"]).
    2. The property name starts with a number.
    3. You want to use a variable to access the property:
      const brand = "Ford";
      console.log(car[brand]); // This looks for "Ford" inside the object
      

In your current lab, since you just created the object, you'll soon be learning how to add these properties so you can practice accessing them!

Would you like to try adding a property to your car object now?

0 Comments

no data
Be the first to share your comment!