What does this Javascript Snippet do (start of jQuery migration file)

I found this piece of code in jQuery Migrate v1.1.1

jQuery.migrateMute===void 0&&(jQuery.migrateMute=!0),function(e,t,n){/* anything */} 

And I really wonder about two things:

1) What does ===void 0 mean?

2) Why are these conditions followed by a comma? My tests showed that it will always be executed.

It just doesn’t need to be known, but I'm really interested because I thought I knew everything about JS.;)

+6
source share
2 answers

void 0 will give undefined , like void X for any X ; it is shorter and cannot be redefined as undefined . So ===void 0 compares jQuery.migrateMute with undefined .

!0 true .

Thus, the "translation" jQuery.migrateMute===void 0&&(jQuery.migrateMute=!0) is equal to:

 if (jQuery.migrateMute === undefined) { jQuery.migrateMute = true; } 

Then the material after the decimal point is executed regardless of this.

+4
source

To summarize all the comments and answers ...

  • void 0 or void(0) is undefined by default. This is why you can use it instead of undefined to make sure it is not overwritten by someone. here is the source

  • And about this design, this means that:

     if(x){y=z};function(){/*...*/}; 
0
source

Source: https://habr.com/ru/post/985486/


All Articles