Optional chaining (?.)
The optional chaining operator ( ?. ) enables you to オプションの目的 read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.オプションの目的
The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish ( null or undefined ), the expression short-circuits with a return value of undefined . When used with function calls, it returns undefined if the given function does not exist.
This results in shorter and simpler expressions when accessing chained properties when the possibility exists that a reference may be missing. It can also be helpful while exploring the content of an object when there's no known guarantee as to which properties are required.
Optional chaining cannot be used on a non-declared root object, but can be used with an undefined root object.
Try it
Syntax
Description
The optional chaining operator provides a way to simplify accessing values through connected objects when it's possible that オプションの目的 a reference or function may be undefined or null .
For example, consider an object obj which has a nested structure. Without optional chaining, looking up a deeply-nested subproperty requires validating the references in between, such as:
The value of obj.first is confirmed to be non- null (and non- undefined ) before then オプションの目的 accessing the value of obj.first.second . This prevents the error that would occur if you accessed obj.first.second directly without testing obj.first .
With the optional chaining operator ( ?. ), however, you don't have to explicitly test and オプションの目的 short-circuit based on the state of obj.first before trying to access オプションの目的 obj.first.second :
By using the ?. operator instead of just . , JavaScript knows to オプションの目的 implicitly check to be sure obj.first is not null or undefined before attempting to access obj.first.second . If obj.first is null or undefined , オプションの目的 the expression automatically short-circuits, returning undefined .
This is equivalent to the following, except that the temporary variable is in fact not created:
Optional chaining with function calls
You can use optional chaining when attempting to call a method which may not exist. This can be helpful, for example, when using an API in オプションの目的 which a method might be unavailable, either due to the age of the implementation or because of a feature which isn't available on the user's device.
Using optional chaining with function calls causes the expression to automatically オプションの目的 return undefined instead of throwing an exception if the method isn't found:
Note: If there is a property with such a name and which is not a function, using ?. will still raise a TypeError exception ( someInterface.customMethod is not a function ).
Note: If someInterface itself is null or undefined , a TypeError exception will still be raised ( someInterface is null ). If you expect that someInterface itself may be null or undefined , you have to use ?. at this position as well: someInterface?.customMethod?.()
Dealing with optional callbacks or event handlers
If オプションの目的 you use callbacks or fetch methods from an object with a destructuring assignment, オプションの目的 you may have non-existent values that you cannot call as functions unless you have tested their existence. Using ?. , you can avoid this extra test:
Optional chaining with expressions
You can also use the optional chaining operator when accessing properties with an expression using the bracket notation of the property accessor:
Optional chaining not valid on the left-hand side of an assignment
It is invalid to try to assign to the result of an optional chaining expression:
Array item access with optional chaining
You can use bracket notation for optional chaining on arrays:
Examples
Basic オプションの目的 example
This example looks for the value of the name property for the member オプションの目的 bar in a map when there is no such member. The result is therefore undefined .
Short-circuiting evaluation
When using optional chaining with expressions, if the left operand is null or undefined , the expression will not be evaluated. For instance:
Stacking the optional chaining operator
With nested structures, it is possible to use optional chaining multiple times:
Combining with the nullish coalescing operator
The nullish coalescing operator may be used after optional chaining in order to build a default value when none was found:
Specifications
Specification |
---|
ECMAScript Language Specification # prod-OptionalExpression |
Browser compatibility
BCD tables only load in the browser
See also
- The Nullish Coalescing Operator
Found a problem with this page?
Last modified: Apr 26, 2022 , by MDN contributors
Your blueprint for a better internet.
Support
Our オプションの目的 communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are オプションの目的 オプションの目的 ©1998– 2022 by individual mozilla.org contributors. Content available under a Creative Commons license.
オプションの目的
LTTW September “EQUALITY in DIVERSITY” Compilation
Visual Artworks in Response to Covid-19
Rhythm Salad Music Clinic [Picture]
NYD at the Museum: SoulSphere of Jakarta [Picture]
A “Tribute” to 2019 Ocean Day
There are no upcoming events at this time.
*Please clear your browser’s cache and refresh to ensure you see the latest version of this site.
Problems with Pornography (are worse than you think!)
UPIC: A Musical オプションの目的 Instrument or A Drawing Device?
The Eco-friendly Traditional Tattoo of Mentawai
COMING SOON
Guitar, Music, Faith, and Brotherhood
What is Art’s role in Society today?
It has long been taken for granted that Art is present in all aspects of human activity and culture. That .
Read More
オプションの目的
Episode 20: Listening to “Gnossiennes” in Paris with Catherine Girardeau
Writer Catherine Girardeau uncovers a story she wrote decades ago about an artist who came into her life and the LP he brought along オプションの目的 with him. This story was recorded at our live show on April 26, 2017 at the Bernal Heights branch of the San Francisco Public Library. Host: Corey Bloom Producer: Ashleyanne Krigbaum … Read more
Eric Mueller of Pirates Press
Pirates Press オプションの目的 is a vinyl production and manufacturing company based out of the Bay Area. Since opening up shop in 2004, they have produced literally millions of records オプションの目的 for hundreds of bands. With their pressing plant in Beroun, Czech Republic the company entertains artists wildest dreams in terms of vinyl production and packaging. For this … Read more
Choosing Data Place Services
When considering the services of a data bedroom, there are many things to consider. Ultimately, you want important computer data room to become easy to use and navigate, however provide secure storage for your documents. Considercarefully what kind of reliability your data space offers and exactly how much control you’ll have above access to delicate … Read more
Next Event
March 4th at Labyrinth Books in Princeton, NJ
After a long hiatus we’re back in action. Stories and songs start at 6:00PM.
オプションの目的
Episode 20: Listening to “Gnossiennes” in Paris with Catherine Girardeau
Writer Catherine Girardeau uncovers a story she wrote オプションの目的 decades ago about an artist who came into her life and the LP he brought along with him. This story was recorded at our live show on April 26, 2017 at the Bernal Heights branch of the San Francisco Public Library. Host: Corey Bloom Producer: Ashleyanne Krigbaum … Read more
Eric Mueller of Pirates Press
Pirates Press is a vinyl production and manufacturing company based out of オプションの目的 オプションの目的 the Bay Area. Since opening up shop in 2004, they have produced literally millions of records for hundreds of bands. With their pressing plant in Beroun, Czech Republic the company entertains artists wildest dreams in terms of vinyl production and packaging. For this … Read more
Choosing Data Place Services
When considering the services of a data bedroom, there are many things to consider. Ultimately, you want オプションの目的 important computer data room to become easy to use and navigate, however provide secure storage for your documents. Considercarefully what kind of reliability your data space offers and exactly how much control you’ll have above access to delicate … Read more
Next Event
March 4th at Labyrinth Books in Princeton, NJ
After a long hiatus we’re back in action. Stories and songs start at 6:00PM.
5Gを目的に契約するなら当面『NTTドコモ』回線一択かも。格安SIMの『無料オプション』使えば激安で手軽に5G環境構築
ハイレゾ音質をストリーミングで楽しむ事が出来る『Amazon Music HD』。サービス開始当初は月額利用料金が『1,980円(税込)』でした。私は同サービスを当時から利用していましたが、『ハイレゾ .
IIJmioはデータ通信専用のeSIMが『440円(税込)~』。ギガプラン、ミニマムスタート、ライトスタート、ファミリーシェア、ケータイ、従量制プランも5Gに対応しています。
mineoはデータ通信専用のシングルタイプ(SMS機能なし)が『880円(税込)~』。音声対応のデュアルタイプでも5Gの利用が可能です。
マイネオなら『NTTドコモ』『Softbank』『au』の中から『5G』環境を『無料』で選び放題!速さと自由が手に入る
『OCNモバイルONE』『IIJmio』『NURO Mobile』など、『5G』サービスを提供する格安SIM(MVNO)が徐々に増えてきました。『mineo』もその内の一つです。 mineoより mi .
【推しSIM】節約派のスマホヘビーユーザーはmineoが最適解!『実用的な通信速度』と『データ大容量』を最重視すべき。
5Gは4Gより高周波なので、4Gを束ねたキャリアアグリゲーションが可能なエリアでは『4G+』を優先する場合が多いかもしれません。
この記事に関連するカテゴリー
格安SIMに関連する記事
【平日昼の通信速度】OCNモバイルONE、UQモバイル、NUROモバイル、楽天モバイル、LINEMOなど合計7社実測!
【6月開催中キャンペーン】mineo、IIJmio、OCNモバイルONE、LINEMO、nuroモバイル、UQモバイル
【試した】OCNモバイルONEはグローバルIPアドレスをプライベートIPアドレスに変えれば本当に電池持ちが良くなるのか
OCNモバイルONEが超久々にセール!OPPO、Xiaomi、AQUOS等人気SIMフリースマホが30日まで割引価格に
楽天モバイルからの乗り換え先候補!データ通信と端末はIIJmio、通話はHISモバイル、通話通信し放題ならmineo!
2022年7月1日から新プランを導入する楽天モバイル。データ通信1GBまでの基本料金が改定され、0GB~3GBまでが一律で『1,078円(税込)』に。 データ量 0GB~1GB 1GB~3GB 3GB .
コメント