TLDR: Optional Chaining Operator is for objects, not properties?
https://www.w3schools.com/jS/js_2020.asp
" The Optional Chaining Operator returns
undefined
if an object isundefined
ornull
(instead of throwing an error)."
/TLDR
I’ve found a quiz question pretty confusing but I thought I would discuss it here first before opening an Issue right away…
EDIT: Link to quiz:
https://www.freecodecamp.org/learn/full-stack-developer/lecture-working-with-objects/what-is-the-optional-chaining-operator-and-how-does-it-work
Here is the question:
What will be the output of the following code?
let car = {
brand: "Toyota",
model: "Corolla"
};
console.log(car?.color);
Toyota
Corolla
undefined
This will throw an error.
Here the question puts the ?.
operator after the object name, which we have not seen in the video. In all of the examples, this operator comes after a property of the object. So already I’m not really sure what the result would be.
Since the operator is after the object name, and not the property I thought it might throw an error when it gets to the property which does not exist, but that’s incorrect:
Incorrect.
The optional chaining operator returnsundefined
if the property doesn’t exist.
Ok, fair enough but I needed to test it because the operator is before the object not the property.
let car = {
brand: "Toyota",
model: "Corolla"
};
console.log(car?.color);
>> undefined
Ok so it’s correct it does return undefined in this case. And if I remove the optional chaining operator?
let car = {
brand: "Toyota",
model: "Corolla"
};
console.log(car.color);
>> undefined
Ok, that’s strange! I thought it was the optional chaining operator that returns undefined and avoids throwing an error?
Maybe it has something to do with the property being last in the chain. How can I generate an error…
let car = {
brand: "Toyota",
model: "Corolla",
color: {
first: "white",
second: "blue"
}
};
console.log(car.colors.third);
>> TypeError: car.colors is undefined
Ok! I have a TypeError thrown when this new object within the object does not exist. Can I supress it with ?.
?
console.log(car.colors?.third);
>> undefined
Success! What about the last node in the chain…
let car = {
brand: "Toyota",
model: "Corolla",
color: {
first: "white",
second: "blue"
}
};
console.log(car.color.third);
>> undefined
Simply returns undefined, with or without the ?.
So, the only way I’m able to generate an error and then suppress it with ?.
is if I use it after an object name, the properties seem to make no difference.
So, the message here: “The optional chaining operator returns undefined
if the property doesn’t exist.” seems to be wrong. A non-existant property seems to always return undefined
.
I see this also corresponds to what w3 says:
" The Optional Chaining Operator returns undefined
if an object is undefined
or null
(instead of throwing an error)." https://www.w3schools.com/jS/js_2020.asp
Example
const car = {type:"Fiat", model:"500", color:"white"};
let name = car?.name;
It’s about objects, not properties.
However, all the examples in the lesson are about properties:
console.log(user.profile?.address?.street); // "123 Main St"
console.log(user.profile?.phone?.number); // undefined