# While and scope

**URL:** <https://forum.freecodecamp.org/t/while-and-scope/95507>\
**Category:** General\
**Created:** [March 10, 2017, 10:17pm UTC](https://forum.freecodecamp.org/t/while-and-scope/95507 "2017-03-10T22:17:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gisesonia](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@gisesonia](https://forum.freecodecamp.org/u/gisesonia)\
**Post date:** [March 10, 2017, 10:17pm UTC](https://forum.freecodecamp.org/t/while-and-scope/95507/1 "2017-03-10T22:17:55Z")

</div>

When I was trying to solve the exercise came up this doubt.

Where I have to put return?  
Because I try one time inside while loop and give an error, outside it’s out of scope.

---

<div class="post-metadata">

**Author:** ![Aloap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/aloap/32/65944_2.png) [@Aloap](https://forum.freecodecamp.org/u/Aloap)\
**Post date:** [March 11, 2017, 12:09am UTC](https://forum.freecodecamp.org/t/while-and-scope/95507/2 "2017-03-11T00:09:29Z")

</div>

The return will end your loop, so you can put it in a while only if you wanted to stop looping because an event occurred.  
You can put it outside. The “out of scope” error is not related to the return itself, but to the variable you’re returning. If you declare it in the while loop, it doesn’t exist anymore once you get out from the loop. So you have to declare that variable outside, or copy it in some global variable before leaving the loop

---

<div class="post-metadata">

**Author:** ![gisesonia](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@gisesonia](https://forum.freecodecamp.org/u/gisesonia)\
**Post date:** [March 13, 2017, 5:10pm UTC](https://forum.freecodecamp.org/t/while-and-scope/95507/3 "2017-03-13T17:10:29Z")

</div>

Thank you, do you know some tutorial to learn more about structure code?

---

<div class="post-metadata">

**Author:** ![Aloap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/aloap/32/65944_2.png) [@Aloap](https://forum.freecodecamp.org/u/Aloap)\
**Post date:** [March 13, 2017, 8:12pm UTC](https://forum.freecodecamp.org/t/while-and-scope/95507/4 "2017-03-13T20:12:51Z")

</div>

Try this:

[https://www.w3schools.com/js/default.asp](https://www.w3schools.com/js/default.asp)

it could be good for a first look. The better I can suggest is any book of programming principles (even based on js). Every different programming language has its own syntax but many of them are really similar, but most important: the basic structure (loop, interruptions and so on) has a common base with almost any language.

---

<div class="post-metadata">

**Author:** ![gisesonia](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@gisesonia](https://forum.freecodecamp.org/u/gisesonia)\
**Post date:** [April 11, 2017, 3:46pm UTC](https://forum.freecodecamp.org/t/while-and-scope/95507/5 "2017-04-11T15:46:23Z")

</div>

Thank you, I know this website.

---

<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, 5:25pm UTC](https://forum.freecodecamp.org/t/while-and-scope/95507/6 "2021-01-16T17:25:35Z")

</div>

Closed on January 16, 2021, 5:25pm UTC


