File tree Expand file tree Collapse file tree 7 files changed +125
-0
lines changed Expand file tree Collapse file tree 7 files changed +125
-0
lines changed Original file line number Diff line number Diff line change 1+ function example ( ) {
2+ var local = "Only available inside example()" ; // with var
3+ global = "Available anywhere once executed" ; // without var
4+ }
5+
6+ console . log ( local ) ;
7+ // "error" (ReferenceError: local is not defined)
8+
9+ console . log ( global ) ;
10+ // "error" (ReferenceError: local is not defined)
11+
12+ example ( ) ; // execute the example function
13+
14+ console . log ( local ) ;
15+ // "error" (ReferenceError: local is not defined)
16+
17+ console . log ( global ) ;
18+ // "Also available anywhere"
Original file line number Diff line number Diff line change 1+ var phrase = "always available" ;
2+
3+ function example ( ) {
4+ var local = "Only available inside example()" ;
5+ console . log ( phrase ) ;
6+ console . log ( local ) ;
7+ }
8+
9+ example ( ) ;
10+ // "always available"
11+ // "Only available inside example()"
12+
13+ console . log ( phrase ) ;
14+ // "always available"
15+
16+ console . log ( local ) ;
17+ // "error" (ReferenceError: local is not defined)
Original file line number Diff line number Diff line change 1+ // one script that uses a counter
2+ count = 5 ;
3+
4+ // another script that sets count based on a price
5+ function thousands ( price ) {
6+ count = price * 1000 ;
7+ return ( count ) ;
8+ }
9+ thousands ( 50 ) ;
10+
11+ // another script that uses count for an iterator
12+ for ( count = 0 ; count < 6 ; count ++ ) {
13+ // do something six times
14+ }
15+
16+ // A whole bunch of code later...
17+
18+ console . log ( count ) ;
19+ // could be anything
Original file line number Diff line number Diff line change 1+ var count = 5 ;
2+
3+ console . log ( count ) ;
4+ // 5
5+ console . log ( window . count ) ;
6+ // 5
7+
8+ window . count = 10 ;
9+ console . log ( count ) ;
10+ // 10
11+ console . log ( window . count ) ;
12+ // 10
Original file line number Diff line number Diff line change 1+ // local to a function
2+ function thousands ( price ) {
3+ var count = price * 1000 ;
4+ }
5+
6+ // immediately invoked function expressions
7+ ( function ( ) { // start a function expression
8+ var count ;
9+ for ( count = 0 ; count < 6 ; count ++ ) {
10+ // do something six times
11+ }
12+ } ( ) ) ; // execute that function expression
13+
14+ // local to a private object (less safe)
15+ var myApp = { } ;
16+ myApp . count = "Hello" . length ;
17+
18+ console . log ( count ) ;
19+ // error: ReferenceError: count is not defined
Original file line number Diff line number Diff line change 1+ function predefined ( ) {
2+ var before = 5 ; // declared and initialized
3+ var after = 10 ; // declared and initialized
4+ return ( before + after ) ;
5+ }
6+
7+ function undefinedAfter ( ) {
8+ var before = 5 ; // declared and initialized
9+ return ( before + after ) ;
10+ var after = 10 ; // initialized, declaration hoisted
11+ }
12+
13+ function undeclaredAfter ( ) {
14+ var before = 5 ; // declared and initialized
15+ return ( before + after ) ;
16+ }
17+
18+ console . log ( predefined ( ) ) ;
19+ // 15
20+
21+ console . log ( undefinedAfter ( ) ) ;
22+ // NaN
23+
24+ console . log ( undeclarededAfter ( ) ) ;
25+ // error: ReferenceError: after is not defined
Original file line number Diff line number Diff line change 1+ var greeting = "Hello" ;
2+ var items = [ "World" ] ;
3+
4+ function greeter ( str , arr ) {
5+ var counter ; // local variable in the function
6+ for ( counter = 0 ; counter < arr . length ; counter ++ ) {
7+ console . log ( str + " " + arr [ counter ] ) ;
8+ }
9+ }
10+
11+ greeter ( greeting , items ) ;
12+ // "Hello World"
13+
14+ console . log ( counter ) ;
15+ // "error" (ReferenceError: counter is not defined)
You can’t perform that action at this time.
0 commit comments