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.
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
If
!s.data && !s.ajaxevaluates totrue, then this case will be selected for execution.switch(true)is the same asswitch(!0)