- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: javascript-best-practices/no-unnecessary-ternary
Language: JavaScript
Severity: Warning
Category: Best Practices
This rule advises against the use of unnecessary ternary operations that return a boolean value. In JavaScript, the ternary operator ? :
is a shorthand way of writing an if-else
statement. However, if the result of the ternary operation is a boolean (such as true
or false
), it is often unnecessary because the condition itself already produces a boolean value.
The use of unnecessary ternary operations can lead to code that is harder to read and understand. Furthermore, it can lead to potential bugs if the ternary operation is not correctly written or understood. To adhere to this rule, you should return the condition itself rather than using a ternary operation.
const foo = bar === 2 ? true : false;
const baz = quux === 3 ? false : true;
const notFoo = foo ? false : true;
call(foo ? foo : 1);
const foo = bar === 2;
const baz = quux !== 3;
const notFoo = !foo;
call(foo || 1);
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products