음악, 삶, 개발

Switch 문의 현명한 사용법 본문

개발 Web/JS

Switch 문의 현명한 사용법

Lee_____ 2021. 3. 11. 01:17

Switch 문 사용시, 매 case 마다 다른 일을 하는것이 아니라,

몇몇 case 는 동일한 함수를 실행할것이라면, 빈 칸을 두고 다음 case 를 작성하면 된다.

그러면 묶어서 하나의 case 로 인식되어, 결과를 호출하게된다.

const str = 'park'

switch (str) {

    case 'lee'  : console.log('cool'); break
    case 'kim'  :
    case 'park' :
    case 'choi' : console.log('bad!'); break
    default : console.log('def')

}