Lesson 19 of 40
Lesson Progress: 0%
const user = {
profile: {
name: "Alice"
}
};
console.log(user.profile?.name);
// AliceInstructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
?. is used to safely check if something exists before using it.user.profile?, the ? checks if profile exists.profile does not exist, the code will not cause an error.?.name part safely tries to get the name inside profile.const user = {};
console.log(user.profile?.name);
// undefinedInstructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
?. symbol to safely peek inside a piece of information without causing an error.user.profile? part asks the computer, "Does a profile exist here first?" before moving forward.?.name, the computer only looks for the name if it successfully found the profile folder first.const numbers = [10, 20];
console.log(numbers[1]);
// 20
console.log(numbers?.[0]);
// undefined
console.log(numbers?.[2]);
// undefinedInstructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
numbers? part asks the computer, "Is there even a list here?" before it tries to look inside.?.[2] tells the computer to safely check for the third item without panicking if the list is too short.?. makes sure you don't get upset if your hand comes back empty!const user = {
greet() {
return "Hello!";
}
};
console.log(user.greet?.());
// Hello!Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
user.greet? part asks the computer, "Does this user actually know how to greet people?" before trying to do it.?.() part is the magic button that only clicks if the function is ready and waiting to be used.const user = {};
console.log(user.greet?.());
// undefinedInstructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
?. as a safety shield to check if a command exists before trying to run it.user.greet? part asks the computer, "Is there a greeting command inside this user?"?.() part tells the computer to only "push the button" to start the action if it actually found one.