What is the difference between using a semicolon, an exclamation mark, or a plus sign at the beginning of JavaScript files?

In JavaScript files, I saw these 3 forms:

;(function() { // content ... }()); 

and

 !function() { // content ... }(); 

Or in the Bootstrap js file:

 +function() { // content ... }(); 

I think that ; ! or + is it that if many files are combined together ; ! or + can separate it from the previous contents of the file.

What is the difference between use ; ! or + ? Is one method better than others?

+5
source share
1 answer
 ;(function() { // content ... }()); 

A half-column is completed by an empty statement followed by a regular IIFE . This has no effect, but may be useful as a signage.

 !function() { // content ... }(); 

Exclamation provokes a statement that should be seen as an expression. See Also: What an exclamation mark does before a function . This is a 1-byte shorter IIFE expression method.

 +function() { // content ... }(); 

Very similar to the version of the exclamation mark, both of them cause the following expression to be evaluated as an expression. The difference is how the result of the expression is processed. + makes it convert to a numerical value,! leads to negation of value. In both cases, the result is then discarded - they are virtually the same.

+2
source

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


All Articles