switch(true) gotcha
switch(true) gotcha
I discovered this week a little footgun if you happen to use this bit of syntactic sugar:
const object = { foo: 'bar', } switch (true) { case object && object.foo: { // This will never execute } default: // ... }
The cases in your switch(true)
must actually return true
, not truthy, for the case to execute.
Using switch(true)
is a bit of a controversial coding practice, since it's only meant to replace long if..else if..else
chains. Literally has no additional utility except aesthetics... but I like it nevertheless :joy_cat: