Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switch(!0) What does it mean

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;
}
like image 504
MMeah Avatar asked Aug 10 '12 23:08

MMeah


Video Answer


1 Answers

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)

like image 157
wanovak Avatar answered Oct 05 '22 07:10

wanovak