Variable variable whos got the variable Copyright 2016 Fred McClurg All Rights Reserved Variable Scope Scope Defined Variable scope has to do with where variables are visible in a program ID: 240434
Download Presentation The PPT/PDF document "Variable Scope" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Variable Scope
Variable, variable, who’s got the variable?
© Copyright
2016,
Fred McClurg All Rights ReservedSlide2
Variable ScopeScope Defined:Variable scope has to do with where variables are “visible” in a program.Local Scope:Scope that is local to function and does not have a value outside the function.Global Scope:Scope that is visible inside and outside the function2Slide3
Global Variable ScopeVariables defined outside of the function have global scope.function clobber() { star = "John Wayne"; // note: no var keyword // displays "John Wayne" console.log( "Inside clobber(): " + star );}var star = "Danny Kaye"; // global variable// displays "Danny Kaye"console.log( "Before clobber(): " + star );
clobber(); // function call
// displays "John Wayne"console.log( "After clobber(): " + star );3
globalScope.htmlSlide4
Parameter Variable ScopeVariables passed to the function as arguments have local scope.function clobber( star ) { // local variable parameter // displays "Danny Kaye" console.log( "Inside clobber() before: " + star ); star = "John Wayne"; // displays "John Wayne" console.log( "In clobber() after: " + star );}var star = "Danny Kaye"; // global variable// displays "Danny Kaye"console.log( "Before clobber() call: " + star );
clobber( star ); // variable argument "Passed by Value"
// displays "Danny Kaye"console.log( "After clobber() call: " + star );4
paramScope.htmlSlide5
Local Variable ScopeVariables defined inside of the function have local scope.function clobber() { var star = "John Wayne"; // local variable // displays "John Wayne" console.log( "Inside clobber(): " + star );}var star = "Danny Kaye"; // global variable// displays "Danny Kaye"console.log( "Before clobber(): " + star );
clobber(); // function call
// displays "Danny Kaye"console.log( "After clobber(): " + star );5
localScope.html