<
start
>
clear
copy to:
0
insert
save as:
new page
new last
delete
CSC 130
Scope
Sometimes, when you program, you may want to re-use a variable name. PCerhaps you did it by accident and have something happening you didn't expect. These are both reasons that it is important to understand scopes. var a = 0; var b = 5; function addNum(b){ a += b; return a; } alert(addNum(3)) Consider the block of code above. First we declared two variables, a and b. Then we create a function that increases a by some value, which we also call b. Then we call the function with the parameter 3. It may be unclear why the result is 3 instead of 5. The answer is because of scope. Inside a function like addNum, we can have our own set of variables with any names we like, including ones we have previously used.