๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/Vanilla JS

JS_๊ธฐ๋ณธ ๋ฌธ๋ฒ• (19)_ case๋ฌธ, ๊ฐ์ฒด ํ™œ์šฉ ๋ณ€ํ˜• case๋ฌธ

by KASSID 2023. 3. 7.

๋ชฉ์ฐจ

    728x90

    JS์—์„œ ํ™œ์šฉํ•˜๋Š” case๋ฌธ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž

     

    1. case๋ฌธ

    ํŠน์ • ๊ฐ’์— ๋Œ€ํ•œ ์—ฌ๋Ÿฌ ์˜ต์…˜์ด ์กด์žฌํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

    const option = 1;
    
    switch (option) {
      case 1:
        console.log('1๋ฒˆ');
        break;
      case 2:
        console.log('2๋ฒˆ');
        break;
      case 3:
        console.log('3๋ฒˆ');
        break;
      default:
        console.log('๋ฌดํšจ');
    }
    
    >>> 1๋ฒˆ

    switch ~ case ๋ฌธ์„ ์ด์šฉํ•œ ์˜ˆ์‹œ๋Š” ์œ„์™€ ๊ฐ™๋‹ค.

     

    break

    ์ฃผ์˜ํ•  ์ ์€ break ์ด๋‹ค.

    break๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํ•ด๋‹น case์˜ ๋‹ค์Œ case๊นŒ์ง€ ์—ฐ์†์ ์œผ๋กœ ์ˆ˜ํ–‰์ด ๋œ๋‹ค.

    ๋”ฐ๋ผ์„œ ์„ค๊ณ„์˜๋„๋Œ€๋กœ ์ˆ˜ํ–‰๋˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

     

    defalut

    defalut๋Š” ๋ธ”๋ก๋ฌธ ๊ฐ€์žฅ ์•„๋ž˜์— ์ž‘์„ฑํ•˜๊ณ  ์œ„์˜ case์—์„œ break๊ฐ€ ๋˜์ง€ ์•Š๋Š” ์ด์ƒ ๋ฌด์กฐ๊ฑด ์‹คํ–‰์ด ๋˜๋Š” ๊ตฌ๋ฌธ์ด๋‹ค.

     

    break ๋ˆ„๋ฝ ํ™œ์šฉ

    break๋ฅผ ๋ˆ„๋ฝํ•˜์˜€์„ ๋•Œ ๋‹ค์Œ case๊นŒ์ง€ ์ด์–ด์ง€๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

    ์ด๋ฅผ ์—ญ์œผ๋กœ ์ด์šฉํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ์˜ˆ์‹œ 1)

    const lastNum = 2;
    
    switch (lastNum) {
      case 0: case 1:
        team = 'A'; break;
      case 2: case 3:
        team = 'B'; break;
      case 4: case 5:
        team = 'C'; break;
      case 6: case 7:
        team = 'D'; break;
      case 8: case 9:
        team = 'E'; break;
      defalut:
        team = '์˜ค๋ฅ˜'; break;
    }
    
    console.log(team);
    
    >>> B

    ์œ„์ฒ˜๋Ÿผ ์˜๋„์ ์œผ๋กœ case๋ฌธ์„ ํ๋ฅด๊ฒŒ ํ•˜์—ฌ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋„์ถœ๋˜๋„๋ก ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

     

    ํ•˜๋‚˜์˜ ์˜ˆ์‹œ๋ฅผ ๋” ์‚ดํŽด๋ณด์ž!

     

    ์˜ˆ์‹œ 2)

    const startMonth = 1;
    let quarter = 0;
    let birthday = '๋ถ„๊ธฐ ๋‚ด ์ƒ์ผ์ž :'
    
    switch (startMonth) {
      case 1:
        birthday += ' Tom';
      case 2:
      case 3:
        birthday += ' Ham';
        birthday += ' Sam';
        quarter = 1;
        break;
        
      case 4:
      case 5:
        birthday += ' Tery';
      case 6:
        quarter = 2;
        break;
        
      case 7:
        birthday += ' Rooney';
      case 8:
        birthday += ' Mac';
      case 9:
        quarter = 3;
        break;
        
      case 10:
        birthday += ' David';
        birthday += ' Phill';
      case 11:
      case 12:
        birthday += ' Denis';
        quarter = 4;
        break;
        
      default:
        birthday = '์œ ํšจํ•˜์ง€ ์•Š์Œ';
    }
    
    console.log(quarter+birthday);
    
    >>> 1๋ถ„๊ธฐ ๋‚ด ์ƒ์ผ์ž : Tom Ham Sam

    ์œ„์ฒ˜๋Ÿผ ๊ฐ’์„ ๋”ํ•ด๊ฐ€๋ฉฐ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     


    2. ๊ฐ์ฒด ํ™œ์šฉ ๋ณ€ํ˜• case

    ์œ„์ฒ˜๋Ÿผ switch ~ case ๋ฌธ์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์กด์žฌํ•œ๋‹ค.

     

    let menu = '๊น€๋ฐฅ';
    
    const price = {
      ๊น€๋ฐฅ : '3000',
      ๋–ก๋ผ๋ฉด : '4000',
      ๊น€์น˜์ฐŒ๊ฐœ : '6000',
      ๊ฐˆ๋น„์ฐœ : '7000',
    }[menu] ?? '์˜ค๋ฅ˜';
    
    console.log(price);
    
    >>> 3000

     

    ๊ฐ์ฒด๋Š” ๋งŒ์•ฝ ์กด์žฌํ•˜์ง€ ์•Š๋Š” key๊ฐ’์„ ์ธ๋ฑ์‹ฑํ–ˆ์„ ๋•Œ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    ์ด๋ฅผ ์ด์šฉํ•ด ?? (null ๋ณ‘ํ•ฉ) ์—ฐ์‚ฐ์ž๋กœ case์™€ default๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

    ๋Œ“๊ธ€