Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  12] [ 2]  / answers: 1 / hits: 144492  / 13 Years ago, thu, july 7, 2011, 12:00:00

In chrome's console, when I type:



> switch(3){default:OK}
OK


So looks like the switch statement has a return value. But when I do:



> var a = switch(3){default:OK}


It throws a syntax error Unexpected Token switch



Is it possible to capture the return statement of the switch?


More From » javascript

 Answers
36

That's because when you're putting that into the Chrome console, you're short-circuiting it. It's just printing 'OK' because it's reaching the default case, not actually returning something.



If you want something returned, stick it in a function, and return the 'OK' from in the default case.



function switchResult(a){
switch(a){
default:
return OK;
}
}

var a = switchResult(3);

[#91311] Tuesday, July 5, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
katharinek

Total Points: 302
Total Questions: 105
Total Answers: 123

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
katharinek questions
Sat, Jan 16, 21, 00:00, 3 Years ago
Sat, Dec 5, 20, 00:00, 4 Years ago
Mon, Nov 30, 20, 00:00, 4 Years ago
Fri, Aug 21, 20, 00:00, 4 Years ago
;