I saw a piece of code that stuck me as odd. What does switch(!0) mean in javascript? What are some cases where this technique would be useful to use?
jsTree uses it in a few places but it looks foreign. I'm sure it has a good reason behind it, but can't figure it out.
http://www.jstree.com/
Here is a clip of code:
switch(!0) {
case (!s.data && !s.ajax): throw "Neither data nor ajax settings supplied.";
case ($.isFunction(s.data)): //...
break;
}
It's comparing each of the cases to boolean true
.
Elaborating
case (!s.data && !s.ajax)
If !s.data && !s.ajax
evaluates to true
, then this case will be selected for execution.
switch(true)
is the same as switch(!0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With