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:)

asked Sep 21, 2012 at 7:27
4
  • 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(){}())... Commented Sep 21, 2012 at 7:31
  • check this out. stackoverflow.com/questions/6645766/… Commented Sep 21, 2012 at 7:39
  • @theintersect - That question is asking about immediately invoked function expressions. There is no invokation of the function in this question. Commented Sep 21, 2012 at 7:41
  • indeed, but it is very helpful when he does invoke. peter.michaux.ca/articles/an-important-pair-of-parens. But thanks for that, i may have overlooked his question. Commented Sep 21, 2012 at 7:51

5 Answers 5

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.

answered Sep 21, 2012 at 7:35
Sign up to request clarification or add additional context in comments.

Comments

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

answered Sep 21, 2012 at 7:33

2 Comments

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.
Also nice to know that in this case you would not get the reference to a, you need to explicitly return some value
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)))));
answered Sep 21, 2012 at 7:34

Comments

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/

answered Sep 21, 2012 at 7:45

Comments

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?

answered Sep 21, 2012 at 7:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.