# You Don’t Know JavaScript, Scope and Closures Chapter 5 Question, this code gives me an error

**URL:** <https://forum.freecodecamp.org/t/you-don-t-know-javascript-scope-and-closures-chapter-5-question-this-code-gives-me-an-error/688463>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 3, 2024, 2:03pm UTC](https://forum.freecodecamp.org/t/you-don-t-know-javascript-scope-and-closures-chapter-5-question-this-code-gives-me-an-error/688463 "2024-05-03T14:03:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yaranour333](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yaranour333/32/338358_2.png) [@yaranour333](https://forum.freecodecamp.org/u/yaranour333)\
**Post date:** [May 3, 2024, 2:03pm UTC](https://forum.freecodecamp.org/t/you-don-t-know-javascript-scope-and-closures-chapter-5-question-this-code-gives-me-an-error/688463/1 "2024-05-03T14:03:39Z")

</div>

This code is from YDKJS book part 2 Scope and Clousers, when I run it it gives me an typeError that fn is not a function at bar(); why so?

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

function bar(fn) {
  fn();
}

foo();
bar();
```

---

<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:** [May 3, 2024, 2:05pm UTC](https://forum.freecodecamp.org/t/you-don-t-know-javascript-scope-and-closures-chapter-5-question-this-code-gives-me-an-error/688463/2 "2024-05-03T14:05:25Z")

</div>

> [@yaranour333](#):
>
> ```auto
> function bar(fn) {
> fn();
> }
> 
> ```

`fn` is the function parameter

> [@yaranour333](#):
>
> `bar();`

you are trying to call `undefined`, `undefined` is not a function

---

<div class="post-metadata">

**Author:** ![yaranour333](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yaranour333/32/338358_2.png) [@yaranour333](https://forum.freecodecamp.org/u/yaranour333)\
**Post date:** [May 3, 2024, 2:10pm UTC](https://forum.freecodecamp.org/t/you-don-t-know-javascript-scope-and-closures-chapter-5-question-this-code-gives-me-an-error/688463/3 "2024-05-03T14:10:31Z")

</div>

I have added  
`var fn;`  
at the beginning of the code and still get the same result

---

<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:** [May 3, 2024, 2:11pm UTC](https://forum.freecodecamp.org/t/you-don-t-know-javascript-scope-and-closures-chapter-5-question-this-code-gives-me-an-error/688463/4 "2024-05-03T14:11:53Z")

</div>

It’s a question of scope, `fn` is the name of the parameter of `bar`

> [@yaranour333](#):
>
> ```auto
> function bar(fn) {
> fn();
> }
> 
> ```

so when `bar` is called `fn` takes the value of whatever is passed as argument,

> [@yaranour333](#):
>
> `bar();`

no argument means that `fn` is undefined.

This `fn` has no relation with the `fn` in the global scope

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [November 2, 2024, 2:12am UTC](https://forum.freecodecamp.org/t/you-don-t-know-javascript-scope-and-closures-chapter-5-question-this-code-gives-me-an-error/688463/5 "2024-11-02T02:12:27Z")

</div>

Closed on November 2, 2024, 2:12am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
