# Why is scope concept not working here , kindly guide

**URL:** <https://forum.freecodecamp.org/t/why-is-scope-concept-not-working-here-kindly-guide/172867>\
**Category:** General\
**Created:** [February 5, 2018, 11:13am UTC](https://forum.freecodecamp.org/t/why-is-scope-concept-not-working-here-kindly-guide/172867 "2018-02-05T11:13:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![amitshrivastavafcc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amitshrivastavafcc/32/65932_2.png) [@amitshrivastavafcc](https://forum.freecodecamp.org/u/amitshrivastavafcc)\
**Post date:** [February 5, 2018, 11:13am UTC](https://forum.freecodecamp.org/t/why-is-scope-concept-not-working-here-kindly-guide/172867/1 "2018-02-05T11:13:18Z")

</div>

Hi,  
I was going through an exercise online and was once again stumped by a simple logic in terms of how to decide scope in JS …

```auto
var foo = "bar";

function bar() {
console.log(foo);
 var foo = "baz";
}

bar()

```

Now here the output is --\> undefined

Why is that so , if i remove and make the code like below

```auto
var foo = "bar";

function bar() {
console.log(foo);

}

bar()

```

The output is -----\> bar

Why does i get undefined even though i have declared var foo = “baz” in the first case, why it can’t just look one line below in its scope of function and give the result

rather it gives undefined , if can afford to look outside of function scope why not just look one line below in same function scope …

I am totally confused , am i not thinking properly or its the way JS is …Thanks

---

<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:** [February 5, 2018, 11:16am UTC](https://forum.freecodecamp.org/t/why-is-scope-concept-not-working-here-kindly-guide/172867/2 "2018-02-05T11:16:31Z")

</div>

The first snippet prints `undefined` because at the point where `console.log` runs, the `foo` variable in the `bar` function is still yet to be assigned a value. It can’t just “look at the next line” to see `foo`'s value.

---

<div class="post-metadata">

**Author:** ![amitshrivastavafcc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amitshrivastavafcc/32/65932_2.png) [@amitshrivastavafcc](https://forum.freecodecamp.org/u/amitshrivastavafcc)\
**Post date:** [February 5, 2018, 11:20am UTC](https://forum.freecodecamp.org/t/why-is-scope-concept-not-working-here-kindly-guide/172867/3 "2018-02-05T11:20:29Z")

</div>

@kevcomedia - thanks, so you mean to suggest we need to go line by line , and hence in every case where is value is declared in a particular scope after it being used will give undefined …

```auto
function bar() {

 var foo = "baz";

console.log(foo);
}

```

like in above code it works , so in any scope if the variable is declared below its use, it will give undefined and also not look out of its scope ? Kindly guide …

---

<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:** [February 5, 2018, 11:23am UTC](https://forum.freecodecamp.org/t/why-is-scope-concept-not-working-here-kindly-guide/172867/4 "2018-02-05T11:23:21Z")

</div>

The code you posted is what the code will look like after the variable declarations have been hoisted at the top… well, almost. _Only the declaration itself is hoisted, not the assignment._

It actually looks like this:

```auto
function bar() {
  // the declaration is hoisted; the assignment is left where it is
  var foo;

  console.log(foo); // `foo` is still undefined at this point.
  foo = 'baz';
}

```

---

<div class="post-metadata">

**Author:** ![amitshrivastavafcc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amitshrivastavafcc/32/65932_2.png) [@amitshrivastavafcc](https://forum.freecodecamp.org/u/amitshrivastavafcc)\
**Post date:** [February 5, 2018, 11:25am UTC](https://forum.freecodecamp.org/t/why-is-scope-concept-not-working-here-kindly-guide/172867/5 "2018-02-05T11:25:43Z")

</div>

@kevcomedia - thanks, that is wonderfully explained , now i get it , but just one last query just if you wish to answer, what is the benefit of doing like this ?

---

<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:** [February 5, 2018, 12:01pm UTC](https://forum.freecodecamp.org/t/why-is-scope-concept-not-working-here-kindly-guide/172867/6 "2018-02-05T12:01:04Z")

</div>

What do you mean? By writing code like I did? I don’t think you’ll actually write code like that, since usually you’d place variable assignments before they’re used anyway. It’s just a demonstration of how JS works with variable declarations.

I don’t know if I answered your question. Please clarify if I didn’t.

---

<div class="post-metadata">

**Author:** ![amitshrivastavafcc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amitshrivastavafcc/32/65932_2.png) [@amitshrivastavafcc](https://forum.freecodecamp.org/u/amitshrivastavafcc)\
**Post date:** [February 5, 2018, 12:18pm UTC](https://forum.freecodecamp.org/t/why-is-scope-concept-not-working-here-kindly-guide/172867/7 "2018-02-05T12:18:26Z")

</div>

@kevcomedia - no i understand one does not write code like you explained, i was just asking why JS which claims to be compiled language as suggested by kyle simpson as well as crockford, two famous authors on JS …

What is the point of restricting itself from looking down in the same scope for a variable value and print it , rather then print undefined … Thanks

---

<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:** [February 5, 2018, 12:29pm UTC](https://forum.freecodecamp.org/t/why-is-scope-concept-not-working-here-kindly-guide/172867/8 "2018-02-05T12:29:55Z")

</div>

> [@amitshrivastavafcc](#):
>
> What is the point of restricting itself from looking down in the same scope for a variable value and print it , rather then print undefined

As far as I can tell, most languages execute lines from top to bottom. Though the variable is indeed in scope, it so happens that the `console.log` line runs before the variable is assigned a meaningful value. I don’t think there’s any more to it other than the ordering of the lines.

---

<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 17, 2021, 8:27pm UTC](https://forum.freecodecamp.org/t/why-is-scope-concept-not-working-here-kindly-guide/172867/9 "2021-01-17T20:27:55Z")

</div>


