3



I have a doubt regarding functions(objects) in javascript.

I have 2 pieces of code like,

    var a= function(){
    console.log('abc')
     }


and

     var a= (function(){
     console.log('abc')
     })


what is the difference between these two>>
thanx:)

Navaneeth
  • 2,555
  • 1
  • 18
  • 38
  • No difference whatsoever. The parenthesis aren't doing anything useful there. You could do `(((function(){})))` and it'll still be the same thing, unless what you mean is `(function(){}())`... – elclanrs Sep 21 '12 at 07:31
  • check this out. http://stackoverflow.com/questions/6645766/why-are-parenthesis-used-to-wrap-a-javascript-function-call – theintersect Sep 21 '12 at 07:39
  • @theintersect - That question is asking about immediately invoked function expressions. There is no invokation of the function in this question. – James Allardice Sep 21 '12 at 07:41
  • indeed, but it is very helpful when he does invoke. http://peter.michaux.ca/articles/an-important-pair-of-parens. But thanks for that, i may have overlooked his question. – theintersect Sep 21 '12 at 07:51

5 Answers5

1

There is no difference now, but if you add another parenthesis () after second function it will run function without invocation.

 var a= (function(){
     console.log('abc')
     })()

it will print 'abc' straightaway

Roman
  • 504
  • 4
  • 10
  • 1
    You will also be able to do var a= function(){ console.log('abc') }() So I think there is no difference there as well. The only subtle difference I know of is in the requirement of a function name. See my answer if you're interested. – Willem Mulder Sep 21 '12 at 07:46
  • Also nice to know that in this case you would not get the reference to a, you need to explicitly return some value – Roman Sep 21 '12 at 08:50
1

There is no difference. You have a function expression, and you can put any number of parentheses around an expression.

Just as this:

a = 42;

is the same as this:

a = (42);

and this:

a = (((((42)))));
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

There is no practical difference. They will both result in an anonymous function being assigned to a.

The first is a "simple assignment". In the second, the parentheses are acting as a "grouping operator", which does one thing:

The production PrimaryExpression : ( Expression ) is evaluated as follows:

  • Return the result of evaluating Expression. This may be of type Reference.

So the grouping operator will return the function contained within it, and assign it to a, just like the first example.

Community
  • 1
  • 1
James Allardice
  • 164,175
  • 21
  • 332
  • 312
1

There is a difference.

( and ) constitute a grouping. A grouping can only contain an expression, thus any function inside it will be an expression and not a declaration.

Since ECMA specifies that a function declaration must always have a function name, and a function expression may omit it, you will be able to do the following

(function() {});

while the following declaration is not valid

function() {};

It won't matter that often, but still... Read this in-depth article for more: http://kangax.github.com/nfe/

Willem Mulder
  • 12,974
  • 3
  • 37
  • 62
0

As has been said, the two examples you've given are essentially the same.

This question has a lot more information contained in the answers regarding parentheses usage, might be worth your time to spin through!

Why are parenthesis used to wrap a javascript function call?

Community
  • 1
  • 1
Scott
  • 2,753
  • 1
  • 24
  • 31