# Variable Scope

**URL:** https://forum.kirupa.com/t/variable-scope/645072
**Category:** programming
**Created:** [November 25, 2020, 11:45pm UTC](https://forum.kirupa.com/t/variable-scope/645072 "2020-11-25T23:45:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![closeupman](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/closeupman/32/12919_2.png) [@closeupman](https://forum.kirupa.com/u/closeupman)
#### Post date: [November 25, 2020, 11:45pm UTC](https://forum.kirupa.com/t/variable-scope/645072/1 "2020-11-25T23:45:31Z")

</div>

If I run this code with isThePrice2 function, nothing after the alert(warning) is encountered in the function will print out.  
I only get alerts for 4 and 5. Then NOTHING! If I take out isThePrice2 function, then isThePrice3 function will output. Does the out of scope in isThePrice2 screw up the program somehow?

Thanks.

\<!doctype html\>

```
<head>

    <meta charset="utf-8">

    <title>Scope</title>

    <style>

       

    </style>

</head>

```

```
<script>

   function isThePriceRight(cost) {

       let total=cost++;

       if (total > 3) {

           alert(total);

       } else {

           alert("not enough");

       }

   }

   isThePriceRight(4);

   // this function screws up the output of everything after alert(warning)

   function isThePriceRight2(cost) {

       let total=cost++;

       if(total>3) {

           let warning=true;

           alert(total);

       } else {

           alert("not enough 2");

       }

       alert(warning);

   }

   isThePriceRight2(5);

   // this function will not output an alert unless I remove the function isThePriceRight2
   function isThePriceRight3(cost) {

       let total=cost++;

       if(total>3) {

           var warning=true;

           alert(total);

       } else {

           alert("not enough 2");

       }
       
       alert(warning);

   }

   isThePriceRight3(6);

   alert("HELP!!! This is never output");

</script>

```

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [November 26, 2020, 1:59am UTC](https://forum.kirupa.com/t/variable-scope/645072/2 "2020-11-26T01:59:08Z")

</div>

Usually when something like that happens it’s because of an error. Check the JavaScript console for errors to see what went wrong. I’ll give you a hint: warning

---

<div class="post-metadata">

### Author: ![closeupman](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/closeupman/32/12919_2.png) [@closeupman](https://forum.kirupa.com/u/closeupman)
#### Post date: [November 26, 2020, 3:08am UTC](https://forum.kirupa.com/t/variable-scope/645072/3 "2020-11-26T03:08:57Z")

</div>

Ya got it. Thanks. Didn’t realize that stops the whole program.  
Got more info as well on bottom of page 91.
