article thumbnail image
Published 2022. 2. 11. 06:24

1. switch문 

보통은 if문을 사용하면됨 
but 코드가 조건의 내용이 어떠한 값으로 정확히 떨어진다면
if문 보다는 switch문이 직관적이라 좋음
break를 반드시 붙여야하고, else부분은 default를 사용하여 마무리함.
const a = random()

예제)if문 

if (a === 0) {
  console.log('a is 0')
} else if (a === 2) {
  console.log('a is 2')
} else if (a === 4){
  console.log('a is 4')
} else {
  console.log('reat....')
}

예제)switch문

switch (a) {
  case 0:
    console.log('a is 0')
    break
  case 2:
    console.log('a is 2')
    break
  case 4:
    console.log('a is 4')
    break
  default: console.log('reat....')
}

'개발이야기 > JS' 카테고리의 다른 글

[JS] Truthy & Falsy 형 변환 (Type conversion)  (0) 2022.02.11
[JS] 반복문 (for)  (0) 2022.02.11
[JS] 연산자  (0) 2022.02.11
[JS] DOM API  (0) 2022.01.31
[JS] 조건문 (if, else)  (0) 2022.01.31
복사했습니다!