Consider the following code:
switch (doSomething()) {
case "1":
return 1;
case "2":
return 2;
default:
console.log(RETURNED_VALUE); // I want here to access the value returned from doSomething
}
I want to print the value returned from doSomething, without storing it first in a variable. Is it possible? (something similar to this)
Thank you
Good night!
Switch is a conditional structure, like if or ternary operator. It's only a set of instructions, not a object allocated in memory. Because of this, you can't have a "this" feature inside of it. However, if you don't want to store the variable for long time, you can just wrap the switch inside a function, like that:
function switchDebug () {
let value = doSomething();
switch (value) {
case "1":
return 1;
case "2":
return 2;
default:
console.log(value);
}
}
This way, the variable will be cleaned after use of function, because it will be a local scope variable of the function.