Optional Chaining (?.) Refactoring

The optional chaining operator returns the value of an object property when the object is available and undefined otherwise. .? is similar to the standard . chaining operator, with an added check if the object is defined.

It enables you to write conci…


This content originally appeared on DEV Community and was authored by Lars Grammel

The optional chaining operator returns the value of an object property when the object is available and undefined otherwise. .? is similar to the standard . chaining operator, with an added check if the object is defined.

It enables you to write concise and safe chains of connected objects when some of those objects can be null or undefined. Before the introduction of optional chaining in ES2020, the && operator was often used to check if an object is available (obj && obj.value).

You can simplify existing checks to use the optional chaining pattern, for example:

  • Change x && x.a to x?.a
  • Change x != null && x.a to x?.a
  • Change x !== null && x !== undefined && x.a to x?.a
  • Change x && x.a && x.a.b && x.a.b.c && x.a.b.c.d to x?.a?.b?.c?.d

One thing to be aware of is that this refactoring replaces falsy checks with nullish checks. For example, when a && a.b is replaced with a?.b, it changes the execution for certain types, e.g. the empty string "" is falsy but not nullish.

However, in many cases these semantic changes will lead actually to more correct behavior. For example, text && text.length will return the empty string, but not its length, whereas text?.length will return 0 for the empty string.

Learn More: Optional Chaining (MDN), Nullish (MDN), Truthy (MDN), Falsy (MDN)

P42 now supports converting many of the above checks into the optional chaining pattern. The refactoring is available on the playground and for all repositories. Try it out!


This content originally appeared on DEV Community and was authored by Lars Grammel


Print Share Comment Cite Upload Translate Updates
APA

Lars Grammel | Sciencx (2021-05-15T13:34:23+00:00) Optional Chaining (?.) Refactoring. Retrieved from https://www.scien.cx/2021/05/15/optional-chaining-refactoring/

MLA
" » Optional Chaining (?.) Refactoring." Lars Grammel | Sciencx - Saturday May 15, 2021, https://www.scien.cx/2021/05/15/optional-chaining-refactoring/
HARVARD
Lars Grammel | Sciencx Saturday May 15, 2021 » Optional Chaining (?.) Refactoring., viewed ,<https://www.scien.cx/2021/05/15/optional-chaining-refactoring/>
VANCOUVER
Lars Grammel | Sciencx - » Optional Chaining (?.) Refactoring. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/15/optional-chaining-refactoring/
CHICAGO
" » Optional Chaining (?.) Refactoring." Lars Grammel | Sciencx - Accessed . https://www.scien.cx/2021/05/15/optional-chaining-refactoring/
IEEE
" » Optional Chaining (?.) Refactoring." Lars Grammel | Sciencx [Online]. Available: https://www.scien.cx/2021/05/15/optional-chaining-refactoring/. [Accessed: ]
rf:citation
» Optional Chaining (?.) Refactoring | Lars Grammel | Sciencx | https://www.scien.cx/2021/05/15/optional-chaining-refactoring/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.