1

I'm trying to learn JavaScript but couldn't understand the difference between the following lines of code.

var f = myfunction;
var k = myfunction();
 function myFuncton()
 {
 alert("hello world");
 }

Since we can't do stuff like this in a managed language like C#. But I have seen so many JavaScript code examples where a function is assigned to a variable without open and close parenthesis before semicolon(;) and then the same function is assigned to another variable with open and close parenthesis as shown in the code above. What is the difference between these two assignments and why do we do that in JavaScript?

asked May 30, 2015 at 14:36
3
  • 1
    OFC you can do both in C# (ie assigning function reference into variable VS assigning result of function call into variable) just with different syntax.. Commented May 30, 2015 at 14:50
  • Can u please share some code as to how one can achieve the same thing like var f = myfunction; in c#? Commented May 30, 2015 at 17:22
  • This is how you can declare variable to hold function reference in C#: public delegate int OperationDelegate(int p); public class MyClass { private int Square(int p) { return p*p; } public void Run() { OperationDelegate func; func = Square; func(10); func = i => i*100; func(10); } } Commented Jun 1, 2015 at 9:38

2 Answers 2

4

Parentheses in an assignment like this:

var k = myfunction();

mean that what's being assigned to k is not the function itself, but rather the result of calling the function — the function's return value, in other words.

Without the parentheses, then you are indeed assigning a reference to a function to some variable:

var f = myfunction;

After doing that, it'll be possible to call the function by either name:

f(); // same as myfunction();

Functions in JavaScript are just a special type of object, but they really are just objects in most ways. They can have properties, and references to them can be passed around exactly in the same way one passes around references to objects.

What makes a function special is that you can call it. A reference to a function followed by () (or () with arguments) is a function call no matter where that reference came from. That's why an assignment of a function call to a variable, or passing a function reference as an argument in a call to some other function, is useful.

answered May 30, 2015 at 14:38
Sign up to request clarification or add additional context in comments.

2 Comments

As a matter of fact I know that but assignment to variable f is more confusing to me.
@user2913184 I just updated the answer; I'll add a few more words too.
2
var f = myfunction;

This creates a new variable f which refers to myfunction.

var k = myfunction();

This creates a variable k which is assigned the value that results after calling and executing myfunction().

answered May 30, 2015 at 14:39

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.