Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is . in JavaScript

The Mystery of the Dot (.) in JavaScript

In the land of JavaScript, the dot (.) is a hardworking, versatile character that often goes unnoticed. It's like the silent hero who's always there, yet seldom appreciated. Today, we're going to shine a spotlight on this humble character and learn what it's all about.

The Role of the Dot

In JavaScript, the dot is an operator, a symbol that performs an operation on some values. It's similar to how the plus (+) operator adds two numbers together. But instead of doing math, the dot operator is a way to access properties (which can be thought of as characteristics or attributes) of objects.

For instance, let's imagine you have a dog. This dog has properties like name, breed, color, and so on. If you were to represent this dog as a JavaScript object, it might look something like this:

let dog = {
  name: "Max",
  breed: "Bulldog",
  color: "Brindle"
};

The dot operator allows you to access these properties. If you want to know the dog's name, you just need to use the dot operator:

console.log(dog.name);  // Outputs: Max

The Dot and Methods

The dot operator isn't just limited to properties. It also allows you to access methods. Methods are actions that objects can perform. They're like functions, but are associated with a specific object.

For example, let's add a bark method to our dog object:

let dog = {
  name: "Max",
  newBreed: "Bulldog",
  color: "Brindle",
  bark: function() {
    console.log("Woof!");
  }
};

Now, you can make the dog bark by calling the bark method with the dot operator:

dog.bark();  // Outputs: Woof!

Dot Notation vs Bracket Notation

There's another way to access properties and methods in JavaScript: bracket notation. It works similarly to dot notation—you're just using brackets instead of a dot. Here's how you'd access the name property with bracket notation:

console.log(dog["name"]);  // Outputs: Max

So what's the difference? The key distinction is that bracket notation can be used with variables and strings that can't be used with dot notation. For example, if you have a property name with a space in it (like "favorite food"), you have to use brackets:

let dog = {
  name: "Max",
  breed: "Bulldog",
  color: "Brindle",
  "favorite food": "Bacon"
};

console.log(dog["favorite food"]);  // Outputs: Bacon

The Dot in Prototypes

Another important use of the dot operator is with prototypes. A prototype is like a blueprint for creating objects. When you create an object in JavaScript, it gets a prototype from which it can inherit properties and methods.

For instance, JavaScript has a built-in String prototype that provides methods like toUpperCase and concat. You can use these methods on any string using the dot operator:

let greeting = "hello";
console.log(greeting.toUpperCase());  // Outputs: HELLO

Conclusion

In the bustling city of JavaScript, the dot might seem like an insignificant speck. But much like the tiny nut and bolt that hold a giant machine together, the dot operator plays a vital role in the programming world. It allows us to interact with objects, call methods, and tap into the powerful world of prototypes. So next time you write a dot in your code, give a nod to this silent but indispensable hero, empowering your JavaScript journey one character at a time.