Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is ? in JavaScript

Understanding the '?' (Optional Chaining) in JavaScript

Breaking Down the Basics

In your journey as a JavaScript learner, you might have come across scenarios where you need to access a property of an object. But what if the object or the property does not exist? JavaScript, in its considerate design, provides a solution to handle such cases without throwing an error - and that's where the '?' operator comes into the picture.

The '?' is known as the Optional Chaining operator in JavaScript. It permits reading the value of a property located deep within a chain of connected objects without having to validate that each reference in the chain is valid. In simpler words, it's a way to tell your code, "Hey, try to access this property, but if you can't, that's okay. Just move on."

How Does It Work?

Let's understand this concept with the help of an example. Let's say we have an object user:

let user = {
  name: "John",
  address: {
    street: "Main",
    city: "New York"
  }
};

What if we want to access the user's state, but the object does not have a state property? If we try user.address.state, JavaScript will throw an error because the state property does not exist.

let state = user.address.state; // Error! 

However, the '?' operator can prevent this error. Here's how:

let state = user.address?.state;  

If address exists, it will return the state property. If address does not exist, it will return undefined instead of throwing an error.

Deeply Nested Properties

So far, we've only explored one level of nesting. But what if we have more levels, and any of them might be undefined? The '?' operator can handle this as well:

let user = {
  name: "John",
  address: {
    street: "Main",
    city: "New York"
  }
};

let zip = user.address?.location?.zip;

In the above example, if address or location does not exist, it will return undefined instead of throwing an error. This gives you a safe way to attempt accessing deeply nested properties.

In Combination with Other Operators

The '?' operator can also be used with function calls and array indices:

let user = {
  admin() {
    alert("I am admin");
  }
};

let guest = {};

user.admin?.(); // I am admin

guest.admin?.(); // no error, but nothing happens

In the above example, if the admin function exists, it gets executed. If it doesn't, nothing happens, and no error is thrown.

Conclusion

In conclusion, the '?' operator, also known as the JavaScript Optional Chaining operator, is your safety net when traversing uncertain object properties. It's like a confident explorer with a reliable GPS that knows when to stop and turn around when a path doesn't exist rather than crashing into the unknown. By using this operator, you can make your code more robust and maintainable, and focus on building great things rather than worrying about potential errors. So next time you venture into the depths of an object, remember to take the '?' operator along!