- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: javascript-best-practices/prefer-optional-chain
Language: JavaScript
Severity: Info
Category: Best Practices
The JavaScript optional chaining operator (?.
) allows you to read the value of a property located deep within a chain of connected objects without having to validate that each reference in the chain is valid. This operator helps prevent potential runtime errors when dealing with nested objects that may or may not exist.
Without the optional chaining operator, you would need to perform a check at each level of the nested structure. This can result in verbose and complex code, which is harder to read and maintain. The optional chaining operator short-circuits this process, returning undefined
if a reference is null
or undefined
before reaching the end of the chain.
To adhere to this rule, you should use the optional chaining operator (?.
) whenever you’re dealing with nested objects where a reference might be null
or undefined
.
a && a.b;
a && a['b'];
a && a.b != null;
(a || {}).b;
(a || {})['b'];
!a || !a.b;
!a || !a['b'];
a?.b;
a?.['b']
!a?.b;
!a?.['b'];
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products