Sunday, July 21, 2013

Variables

Global and local

1.<script>
2. var maeWest = "Why don't you come up and see me sometime?.<br>";
4. function didIStutter() {
5. document.write(maeWest);
6. }
7. didIStutter(); 
8. document.write(maeWest);
9.</script>

maeWest is a Global variable. She was declared outside of the function, and so she can be called anytime in the program. This script will cause her to stutter: inviting you up to her place twice. 

If she were declared inside of the function (local), you would not be allowed to call her outside of the function as shown on line 8. In that case you would only receive one invitation.

No comments:

Post a Comment