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;
}
//コンソールには「注意」と「渡れ」が表示される