[英]How to use switch in a function
I'm trying to get an output from a function converted by switch statement:我正在尝试从由 switch 语句转换的函数中获取输出:
function switchItUp(number) { switch (number) { case 1: let output = 'one'; break; default: } return output } console.log(switchItUp(1));
Define output
in the outer scope (function's)在外部范围(函数的)中定义
output
function switchItUp(number) { let output; switch (number) { case 1: output = 'one'; break; default: } return output } console.log(switchItUp(1));
You need to define output variable out of switch and in switch assign value to it您需要在 switch 之外定义输出变量,并在 switch 中为其赋值
function switchItUp(number) {
let output;
switch (number) {
case 1:
output = 'one';
break;
default:
}
return output
}
console.log(switchItUp(1))
or more simple you can return where switch breaks:或者更简单,你可以返回开关中断的地方:
function switchItUp(number) { switch (number) { case 1: return 'one'; default: } } console.log(switchItUp(1))
function switchItUp(number) { switch (number) { case 1: let output = 'one'; return output; default: } } console.log(switchItUp(1));
You will have to define the output
to before the switch statement like below:您必须在 switch 语句之前定义
output
,如下所示:
function switchItUp(number) {
let output;
switch (number) {
case 1:
output = 'one';
break;
default:
}
return output
}
Hope this will works for you希望这对你有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.