What is ?. in JavaScript
Today, we're going to pop the hood on a small but mighty feature in JavaScript: the Optional Chaining operator, denoted as ?.
. Don't worry if the term 'operator' sounds like a jargon. In simple words, operators are tools that allow us to perform actions on things, or more technically, values.
Uncovering the Mysteries of ?.
Think of an operator like a power tool. In the case of the ?.
operator, it's more like a safety tool. It allows us to dig into an object to grab a value. If the value is there, great! If it's not, JavaScript will stop digging and simply return undefined
, instead of throwing an error. Let's see it in action:
let car = {
brand: {
name: 'Tesla'
}
};
console.log(car.brand.name); // Tesla
console.log(car.model?.name); // undefined
In the above example, car.brand.name
exists, so it returns 'Tesla'. But car.model
doesn't exist, so trying to access name
of something that doesn't exist would normally throw an error. However, with ?.
, it returns undefined
safely.
Why Use ?.
Imagine you're trying to find a book in a giant library. You have the specific shelf, row, and even the exact spot where the book should be. But what if the book isn't there? In JavaScript, this would usually result in an error and stop your code from running, just like a librarian would probably stop you from tearing apart the library looking for a missing book. This is where our ?.
operator comes in handy. It's like a kind librarian who kindly informs you that the book isn't there, without causing a scene.
Optional Chaining with Functions and Arrays
The ?.
operator isn't limited to objects. It can be used with functions and arrays as well.
let bike = {
startEngine: function() {
return 'Engine Started';
}
};
console.log(bike.startEngine?.()); // Engine Started
console.log(bike.stopEngine?.()); // undefined
Just like with objects, if the function doesn't exist, JavaScript safely returns undefined
without throwing an error. The same safety measure applies to arrays:
let array = [1, 2, 3];
console.log(array?.[0]); // 1
console.log(array?.[5]); // undefined
The Catch with ?.
While our ?.
operator is very useful, it does have a catch. It only checks if the value is null
or undefined
. Other "falsy" values such as 0
, ''
, NaN
, and false
are considered valid. Here's an example:
let boat = {
speed: 0
};
console.log(boat.speed?.unit); // undefined
Even though boat.speed
is 0
(which is a "falsy" value), JavaScript still tries to access unit
property of it, as ?.
does not consider 0
as undefined
or null
.
Conclusion
The ?.
operator in JavaScript is like a friendly guide in your coding journey. It allows you to explore the terrains of objects, arrays, and functions without the fear of running into a wall (or an error). It's like the padding on the corners of a coffee table, absorbing the impact and preventing crashes. However, remember that it's not a magic wand that makes all problems disappear. It only deals with null
and undefined
, and treats other "falsy" values as valid.
So, the next time your code dives deep into the nested structures, make sure it's equipped with the ?.
safety tool. Happy coding!