Skip to content

Commit 7ef0ff6

Browse files
author
Angela Molina
committed
Added code sample
1 parent cda42b8 commit 7ef0ff6

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

5.3-1.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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"

5.3-2.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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)

5.3-3.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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

5.3-4.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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

5.3-5.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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

5.3-6.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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

5.3.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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)

0 commit comments

Comments
 (0)