Expression inside switch case statement
You could always do
switch (true) { case (amount >= 7500 && amount < 10000): //code break; case (amount >= 10000 && amount < 15000): //code break; //etc...
It works because true
is a constant, so the code under the first case statement with an expression that evaluates to true will be executed.
It's kinda "tricky" I guess, but I see nothing wrong with using it. A simple if/else
statement would probably be more concise, and you'd not have to worry about accidental fall-through. But there it is anyway.
@MooGoo's switch (true)
will give you a Weird condition
error in jsLint, so let's get a little more creative in case that's an issue, and, I think, increase readability a touch.
So we're not evaluating if each case
is true
or false
; we're comparing if that case
's value is equal to our switch
term. So let's take advantage of that by throwing a shorthand if
into our case
statement and return our original switch term if the condition's true.
I'm also including a sort of real world example, where you want to have two "defaults" -- one if your term is outside of your "important" range in the positive direction, and another in case you're in the negative direction.
Key phrase:case (x > 0 ? x : null):
"If my term, x
, is greater than zero, return x
so that x === x
and I take the case branch."
http://jsfiddle.net/rufwork/upGH6/1/
/*global document*//*jslint evil:true*/var x = 10;switch (x) { case (x > 0 ? x : null): document.write('ha ha ha! I fooled switch AND jsLint! Muhahahahaha!'); break; case 0: document.write('zero is nothing.'); break; case -1: document.write('low'); break; case -2: document.write('lower'); break; case -3: document.write('lowest I care about'); break; default: // anything lower than -3. document.write('TOO LOW!!!! (unless you cheated and didn\'t use an int)');}document.write('<br>done.');
Quick reply to @Sv443:
Do notice that the default:
switch says, "unless you cheated and didn't use an int" and that short circuiting requires x === x
when you return x
.
But your point is a useful reminder that NaN
is the only case where short circuiting can't apply.
That is, x
must == x
to short circuit in switch
and, as MDN tells us, "NaN, and only NaN, will compare unequal to itself" (double or triple =
).
That also means that switching on a NaN
value (and only a NaN
value) will always hit default
in ANY switch
because you can't match its value.
Here's the full quote from MDN:
NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.
You could change the default
logic to check what you have:
isNaN(x) ? document.write ('nan') : document.write('TOO LOW!!!! ...)');
Or you even could go full hipster like MDN suggests (but please don't ;^D):
x !== x ? document.write ('nan') : document.write('TOO LOW!!!! ...)');
That is not how a switch
block works. The case
is used to hold a single value that if they are equal to the value on the switch
line. if-else
statements will serve you well.
Here is some information about the switch
block.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch