What is the use of Exclamation Mark (!) before declaring functions in Javascript?

Asked

Viewed 2,317 times

18

I was reading a manual, and I was asked to start the functions like this:

!function (){

}();

This exclamation mark appeared. What is the purpose of it?

  • Why were they negative ?

  • 3

    About the operator itself: http://answall.com/questions/37971/operador-not

  • 1

    I edited the title to highlight the already existing context in the body of the question. About the denial operator, there is already question and answer on the site, would be a duplicate. In the case it is not, because it is specific context (beginning of the line, before Function, objective other than negation)

  • @jbueno There is also my answer: http://answall.com/a/17355

4 answers

26


Try removing the exclamation to see what happens. Open the console. It will give a syntax error, and the function will not be executed.

Javascript syntax interprets the function as a statement only if the line starts with function (ignoring spaces and tabs). Function declarations cannot be immediately executed with () at the end, only expressions can (more on the difference between declaration and expression of functions). So the NOT operator in this case is just a trick to force the interpretation of the function as an expression and allow it to be executed immediately. There are other ways to do this, such as using the unary operator + or parentheses, among others:

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


(function() {
   // ...
}());
  • 4

    I always use the second, it seems easier to understand, excellent answer!

  • Which one would you indicate to use, the ! the + or ()?

  • 1

    In agree with Guilherme, @Virgilionovic. It costs to use parentheses. But if you want to save bytes, you need to go to +, !, etc..

  • Vlw, thank you!.

9

The ! before the function causes it to be treated as an expression, so that we can call it:

!function () {}()

This will also return the boolean opposite of the function return value, in this case true, why!undefined is true.

8

Note this code:

!function (){
    return true;
}();

This is what he produces:

false

Already that other code:

!function (){
    return false;
}();

Produces this:

true

That is, it reverses the function result.

To understand how it works, let’s divide the program into three parts:

  • !
  • function() { ... }
  • ();

And then, let’s start with the second part, function (){ ... }, declaring an anonymous function.

By adding the second part and the third part, ();, we have that the anonymous function declared in the second part will be invoked immediately, producing as a result what is returned by that function.

By attaching the first part, !, with the second and third, we have to reverse the value given as a result of invoking the anonymous function.

5

The ! javascript symbol is used to invert a boolean expression.

In this case, as it precedes a function, if the function returns true, the result will be false and vice versa.

Works as the NOT operator.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.