Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is dot notation in JavaScript

Understanding Dot Notation Fundamentals

If you've ever seen a line of code that looks like, you've seen dot notation in action! Dot notation is a way to access the properties (think characteristics or attributes) of an object in JavaScript. Let's break down what this means using a real-world example.

Imagine you have a car. This car has various properties, such as its color, make, model, and year. In the world of JavaScript, you might represent this car like so:

let car = {
  color: 'red',
  make: 'Toyota',
  model: 'Corolla',
  year: 2019

In this example, car is our object, and color, make, model, and year are its properties. We can access these properties using dot notation.

Accessing Properties with Dot Notation

To access the color property of our car object, we would write car.color.

console.log(car.color); // Outputs: 'red'

It's as simple as that! By using the dot (.), we are telling JavaScript, "Hey, I want to access this specific property of the object!"

Modifying Properties with Dot Notation

Dot notation isn't just for reading properties; we can also modify them. Suppose we want to repaint our car. We can change the color property like so:

car.color = 'blue';
console.log(car.color); // Outputs: 'blue'

With that simple line, we've repainted our car in the digital world!

When to Use Dot Notation

Dot notation is great for its simplicity, but it does have some limitations. You can only use dot notation when the property name is a valid JavaScript identifier. An identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9).

For example, if our car had a property fuel-type, trying to access car.fuel-type would result in an error because - is not valid in an identifier. In these cases, we would use bracket notation, another way to access or set object properties (car['fuel-type']).

Dot Notation and Methods

Properties don't have to be just static information; they can also be functions. When a function is a property of an object, we call it a "method".

Let's add a startEngine method to our car:

let car = {
  color: 'red',
  make: 'Toyota',
  model: 'Corolla',
  year: 2019,
  startEngine: function() {
    console.log('Vroom vroom!');

car.startEngine(); // Outputs: 'Vroom vroom!'

By using dot notation, we can call our startEngine method just like we accessed our properties.

Understanding and Utilizing Dot Notation

Knowing how and when to use dot notation is a critical part of mastering JavaScript. It's the key to interacting with objects, allowing you to read, modify, and even invoke properties.

Remember, dot notation is like the keys to a car. Just as you use a key to start the car's engine or unlock its doors, you use dot notation to unlock the power and potential of objects in JavaScript.


So there you have it! You've journeyed through the ins and outs of dot notation, and just like a smooth drive in a well-oiled car, you've navigated the path with ease. Remember that dot notation is your trusty vehicle when exploring the land of JavaScript objects. It's your tool to access the properties of objects, like peering under the hood of a car to see its engine. So, buckle up, enjoy the ride, and harness the power of dot notation as you cruise through your JavaScript journey!