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
728x90

✨ 열거 가능한 성질의 object의 key를 반복하기 위한 for loop

💻Example Code

const charMap = { a:1, b:2, c:3 };

for(let char in charMap) { console.log(`${char}: ${charMap[char]}`) };

실행 결과(object key를 하나씩 출력)

😋 Map Object 또는 열거 가능한 Object에 대한 반복문을 사용하고 싶을 때 사용 가능하다.
Max Char(가장 많이 쓰인 문자 찾기), Vowels(모음 개수 찾기) 등 Algorithm에서 확인 가능하다.

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

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

Conditional (ternary) operator(삼항 조건 연산자)  (0) 2019.12.30
for...of  (0) 2019.12.27
728x90

✨ index구간 지정없이 String, Array, array-like objects 의 반복문을 실행하기 위한 for loop

💻Example Code

const strArr = [ 'a', 'b', 'c' ];

for( let char of strArr ) { console.log(char); }

실행 결과(Array 값을 하나씩 출력)

😋 Map object를 만들 때, 단순하게 array의 값을 하나씩 받아 활용하고 싶을 때 등 다양한 곳에서 사용 가능하다.
Max Char(가장 많이 쓰인 문자 찾기) Algorithm에서 확인 가능하다.

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

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

Conditional (ternary) operator(삼항 조건 연산자)  (0) 2019.12.30
for...in  (0) 2019.12.27

+ Recent posts