fix(curriculum): updated the optional chaining questions (#63971)

This commit is contained in:
Sagar Kumar
2025-11-22 23:02:12 +05:30
committed by GitHub
parent 5c28fbffa8
commit 5a9b36790e
@@ -68,44 +68,35 @@ Remember, the optional chaining operator is most useful when you're not sure if
## --text--
What will be the output of the following code?
```js
let car = {
brand: "Toyota",
model: "Corolla"
};
console.log(car?.color);
```
What does the optional chaining operator (`?.`) do in JavaScript?
## --answers--
`Toyota`
It assigns a default value when a property is missing.
### --feedback--
The optional chaining operator returns `undefined` if the property doesn't exist.
The optional chaining operator only prevents errors when a value is `null` or `undefined`.
---
`Corolla`
It prevents `undefined` values from appearing in objects.
### --feedback--
The optional chaining operator returns `undefined` if the property doesn't exist.
The optional chaining operator only prevents errors when a value is `null` or `undefined`.
---
`undefined`
It safely accesses nested properties and returns `undefined` instead of throwing an error if a value is missing.
---
This will throw an error.
It forces JavaScript to evaluate missing properties even if they don't exist.
### --feedback--
The optional chaining operator returns `undefined` if the property doesn't exist.
The optional chaining operator only prevents errors when a value is `null` or `undefined`.
## --video-solution--
@@ -116,89 +107,93 @@ The optional chaining operator returns `undefined` if the property doesn't exist
What will be the output of the following code?
```js
let person = {
name: "John",
age: 30
const person = {
name: "Alice",
age: 30
};
console.log(person?.name);
console.log(person.address.street);
```
## --answers--
`John`
---
`30`
### --feedback--
If the property exists, the optional chaining operator allows access to it normally.
---
`undefined`
### --feedback--
If the property exists, the optional chaining operator allows access to it normally.
Accessing a missing nested property without optional chaining stops code execution.
---
This will throw an error.
`street`
### --feedback--
If the property exists, the optional chaining operator allows access to it normally.
Accessing a missing nested property without optional chaining stops code execution.
---
`{}`
### --feedback--
Accessing a missing nested property without optional chaining stops code execution.
---
A `TypeError` is thrown because `person.address` is undefined.
## --video-solution--
1
4
## --text--
What will be the output of the following code?
```js
let book = {
title: "JavaScript 101",
author: {
name: "Jane Doe"
const user = {
name: "John",
profile: {
email: "john@example.com",
"home address": {
street: "123 Main St",
city: "Somewhere"
}
}
};
console.log(book?.author?.name);
console.log(user?.profile?.address?.street);
```
## --answers--
`JavaScript 101`
### --feedback--
The optional chaining operator can be used multiple times in a single expression.
---
`Jane Doe`
---
`undefined`
---
`"123 Main St"`
### --feedback--
The optional chaining operator can be used multiple times in a single expression.
Think about what represents a value that hasn't been assigned.
---
This will throw an error.
`null`
### --feedback--
The optional chaining operator can be used multiple times in a single expression.
Think about what represents a value that hasn't been assigned.
---
A `TypeError` is thrown.
### --feedback--
Think about what represents a value that hasn't been assigned.
## --video-solution--
2
1