728x90

✨ if else 문법을 간단하게 쓰고 싶을 때 사용하는 ternary operator(삼항 조건 연산자)

💻Example Code

const a = 11;
const b = 77;

a < b ? console.log('true') : console.log('false');

실행 결과(a는 b보다 작기 때문에 true를 출력)

😋 if else는 많은 줄을 차지하는 반면에, ternary operator(삼항 연산자)를 사용하면 최소 한 줄로 줄일 수 있다.
간결한 코드 작성을 원할 때, 매우 유용하다.

👉 자세한 내용은 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

 

Conditional (ternary) operator

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execu

developer.mozilla.org

 

'JavaScript > Grammar' 카테고리의 다른 글

for...in  (0) 2019.12.27
for...of  (0) 2019.12.27

+ Recent posts