0
0

More than 3 years have passed since last update.

JavaScript勉強の記録その2: Switch文

Last updated at Posted at 2020-01-04

JavascriptでSwitch文

switchの引数でとった値を判定し、'赤'だった場合は「止まれ」、'黄'だった場合は「注意、'青'だった場合は「渡れ」をコンソールに表示します。

index.js
const signal = ''

switch (signal) {
  case '':
    console.log('止まれ');
    break;
  case '':
    console.log('注意');
    break;
  case '':
    console.log('渡れ');
    break;
}

ちなみにbreakはループ処理を抜ける命令ですので、breakの記述がない場合は下まで処理が続きます。

index.js
const signal = ''

switch (signal) {
  case '':
    console.log('止まれ');
    break;
  case '':
    console.log('注意');
  case '':
    console.log('渡れ');
    break;
}
//コンソールには「注意」と「渡れ」が表示される
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0