You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
...and another example. In Web development, we can get an object that corresponds to a web page element using a special method call, such as`document.querySelector('.elem')`, and it returns `null`when there's no such element.
let html = document.querySelector('.elem').innerHTML; //เกิด Error ถ้าเป็น null
```
Once again, if theelementdoesn't exist, we'll get an error accessing `.innerHTML` of `null`. And in some cases, when the absence of the element is normal, we'd like to avoid theerror and just accept `html = null` as the result.
It works, there's no error... But it's quite inelegant. As you can see, the `"user.address"` appears twice in the code. For more deeply nested properties, that becomes a problem as more repetitions are required.
หรือในกรณี `document.querySelector`:
E.g. let's try getting `user.address.street.name`.
```js run
let html = document.querySelector('.elem') ? document.querySelector('.elem').innerHTML : null;
```
We need to check both `user.address` and`user.address.street`:
E.g. in`user?.address.street.name`the `?.`allows `user`to safely be`null/undefined`(and returns `undefined` in that case), but that's only for `user`. Further properties are accessed in a regular way. If we want some of them to beoptional, then we'll need to replace more`.`with `?.`.
For example, if according to our coding logic`user`object must exist, but`address`is optional, then we should write`user.address?.street`, but not`user?.address?.street`.
So, if `user` happens to be undefined due to a mistake, we'll see a programming error about it and fix it. Otherwise, coding errors can be silenced where not appropriate, and become more difficult to debug.
Here, in both lines we first use the dot (`userAdmin.admin`) to get`admin`property, because we assume that theuser object exists, so it's safe read from it.
Then `?.()` checks the left part: if the admin function exists, then it runs (that's so for `userAdmin`). Otherwise (for `userGuest`) the evaluation stops without errors.
## Optional Chaining กับ [] : `?.[]`
The `?.[]`syntax also works, if we'd like to use brackets `[]` to access properties instead of dot `.`. Similar to previous cases, it allows to safely read a property from an object that may not exist.
As we can see, all of them are straightforward and simple to use. The`?.`checks the left part for`null/undefined`and allows the evaluation to proceed if it's not so.
Still, we should apply`?.`carefully, only where it's acceptable that the left part doesn't exist. So that it won't hide programming errors from us, if they occur.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.