# Assign variable to function VS assign function to variable

**URL:** <https://forum.freecodecamp.org/t/assign-variable-to-function-vs-assign-function-to-variable/149602>\
**Category:** General\
**Created:** [September 24, 2017, 5:04am UTC](https://forum.freecodecamp.org/t/assign-variable-to-function-vs-assign-function-to-variable/149602 "2017-09-24T05:04:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhouxiang19910319](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zhouxiang19910319/32/98849_2.png) [@zhouxiang19910319](https://forum.freecodecamp.org/u/zhouxiang19910319)\
**Post date:** [September 24, 2017, 5:04am UTC](https://forum.freecodecamp.org/t/assign-variable-to-function-vs-assign-function-to-variable/149602/1 "2017-09-24T05:04:47Z")

</div>

```auto
var fn;

function foo() {
	var a = 2;

	function baz() {
		console.log( a );
	}

	fn = baz; // assign `baz` to global variable
}

function bar() {
	fn(); // look ma, I saw closure!
}

foo();

bar(); // 2

```

I know that in the code above, we passed a function to a variable. `fn=baz`, if I change it to `fn=baz()` IDE will give me a type error: fn is not a function. **So I assume in js we do not assign functions to variables, we assign the output of the functions into a variable. Because the elements at the each side of `=` has to be the same type.** Am I correct?

Then what does this mean? `function bar(){fn();}`? why do we need to add a parameter after a variable?

I am a bit confused…

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [September 24, 2017, 5:19am UTC](https://forum.freecodecamp.org/t/assign-variable-to-function-vs-assign-function-to-variable/149602/2 "2017-09-24T05:19:54Z")

</div>

> [@zhouxiang19910319](#):
>
> So I assume in js we do not assign functions to variables, we assign the output of the functions into a variable. Because the elements at the each side of = has to be the same type.

You _can_ assign functions to variables (because in JS you can pass around functions as if they were plain data), like in `var f = function() {...};`. You can also invoke the function in `f` by writing `f()` in your code.

Also, variables in JS have no type. A variable that previously held a number doesn’t care if you assign it a string or array, or a function

When you changed it to `fn = baz()`, you were invoking the `baz` function, which returns `undefined` (simply because there are no return statements in that function). So `fn` now is `undefined`, which is definitely not a function, and you get the error when `fn()` was executed.

---

<div class="post-metadata">

**Author:** ![zhouxiang19910319](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zhouxiang19910319/32/98849_2.png) [@zhouxiang19910319](https://forum.freecodecamp.org/u/zhouxiang19910319)\
**Post date:** [September 24, 2017, 12:58pm UTC](https://forum.freecodecamp.org/t/assign-variable-to-function-vs-assign-function-to-variable/149602/3 "2017-09-24T12:58:08Z")

</div>

> [@kevcomedia](#):
>
> When you changed it to fn = baz(), you were invoking the baz function, which returns undefined (simply because there are no return statements in that function). So fn now is undefined, which is definitely not a function, and you get the error when fn() was executed.

I don’t think that baz function returns undefined because here in this example, it returns 2.

```auto
function baz(){
	var a=2;
	console.log(a);
}

var fn=baz();

```

---

<div class="post-metadata">

**Author:** ![zhouxiang19910319](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zhouxiang19910319/32/98849_2.png) [@zhouxiang19910319](https://forum.freecodecamp.org/u/zhouxiang19910319)\
**Post date:** [September 24, 2017, 1:02pm UTC](https://forum.freecodecamp.org/t/assign-variable-to-function-vs-assign-function-to-variable/149602/4 "2017-09-24T13:02:22Z")

</div>

But to understand the code I put 1st was

1. baz got assigned to fn 1st via `fn=baz`.
2. then baz got invoked via `function bar(){fn();}`.

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [September 24, 2017, 1:17pm UTC](https://forum.freecodecamp.org/t/assign-variable-to-function-vs-assign-function-to-variable/149602/5 "2017-09-24T13:17:10Z")

</div>

Don’t confuse the result of `console.log`ging with return values. The `2` that you see is not a return value, but rather the result of `console.log`ging the 2 from inside `baz()`. When a function has no `return` statements, their return values are implicitly `undefined`.

> [@zhouxiang19910319](#):
>
> But to understand the code I put 1st was
> 
> baz got assigned to fn 1st via fn=baz.  
> then baz got invoked via function bar(){fn();}.

True. The global `fn` variable holds a reference to the `baz` function. Then you can later invoke the `baz` function by using `fn()`

---

<div class="post-metadata">

**Author:** ![zhouxiang19910319](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zhouxiang19910319/32/98849_2.png) [@zhouxiang19910319](https://forum.freecodecamp.org/u/zhouxiang19910319)\
**Post date:** [September 24, 2017, 2:03pm UTC](https://forum.freecodecamp.org/t/assign-variable-to-function-vs-assign-function-to-variable/149602/6 "2017-09-24T14:03:31Z")

</div>

> [@kevcomedia](#):
>
> Don’t confuse the result of console.logging with return values. The 2 that you see is not a return value, but rather the result of console.logging the 2 from inside baz(). When a function has no return statements, their return values are implicitly undefined.

**I will remember this from now on. Thanks!!**

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:34am UTC](https://forum.freecodecamp.org/t/assign-variable-to-function-vs-assign-function-to-variable/149602/7 "2021-01-16T11:34:37Z")

</div>

Closed on January 16, 2021, 11:34am UTC


