Skip to content

Commit 5b0512a

Browse files
committed
Declarations
1 parent 5851922 commit 5b0512a

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

ES6/Declarations/const.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Name: const
3+
* Spec: https://www.ecma-international.org/ecma-262/6.0/#sec-let-and-const-declarations
4+
*
5+
* WARNING / ATTENTION
6+
* 1. Many of the examples in this file will throw an error. Don't try to run this file all at once, run them one by one.
7+
* 2. A few of the examples (dealing with "this" and "window") will only work properly in a browser, not server-side
8+
*
9+
*/
10+
11+
// Declare variables but don't assign them a value;
12+
var var1;
13+
console.log("The declared but unassigned var1: ",var1, "has a type of: ",typeof(var1));
14+
const const1; // This should throw an error
15+
16+
17+
// Declare at higher scope and use at lower scope
18+
const const2 = 1;
19+
console.log("This is const2 before the if-statement ",const2);
20+
if('a' === 'a'){
21+
console.log("This is const2 within the if-statement ",const2);
22+
}
23+
console.log("This is const2 after the if-statement ",const2);
24+
25+
26+
// Declare at higher scope and modify at lower scope, but use again at original scope unchanged
27+
const const3 = 1;
28+
console.log("This is const3 before the if-statement ",const3);
29+
if('a' === 'a'){
30+
const const3 = 2;
31+
console.log("This is const3 within the if-statement ",const3);
32+
}
33+
console.log("This is const3 after the if-statement ",const3);
34+
35+
36+
// Try to modify at the same level it was declared, omitting keyword
37+
const const4 = 1;
38+
const4 = 2;
39+
40+
41+
// Try to modify at the same level it was declared, with keyword
42+
const const5 = 1;
43+
const const5 = 2;
44+
45+
46+
// Declare at inner scope and try to use again at outer scope
47+
console.log("This is const6 before the if-statement ",typeof(const6));
48+
if('a' === 'a'){
49+
const const6 = 2;
50+
console.log("This is const6 within the if-statement ",const6);
51+
}
52+
console.log("This is const6 after the if-statement ",typeof(const6));
53+
54+
55+
// Declare at inner scope and declare again at outer scope
56+
if('a' === 'a'){
57+
const const7 = 2;
58+
console.log("This is const7 within the if-statement ",const7);
59+
}
60+
const const7 = 3;
61+
console.log("This is const7 after the if-statement ",const7);
62+
63+
64+
// Try to access a const that hasn't been declared yet but is declared below
65+
console.log("This is the type of const8 before we declare it",typeof(const8));
66+
const const8 = 1;
67+
68+
// Declare a const object then try to change it to a different object
69+
const const9 = {
70+
'foo' : 'bar'
71+
};
72+
const9 = {
73+
'fizz' : 'buzz'
74+
};
75+
76+
// Declare a const object then try to add a property
77+
const const10 = {
78+
'foo' : 'bar'
79+
};
80+
const10.fizz = 'buzz';
81+
console.log("This is const10 after adding the new property ",const10);
82+
83+
// Declare a const object then try to delete a property
84+
const const11 = {
85+
'foo' : 'bar'
86+
};
87+
delete const11.foo;
88+
console.log("This is const11 after deleting the foo property ",const11);
89+
90+
// Declare a const array then try to change it to a different array
91+
const const12 = ['foo','bar'];
92+
const12 = ['fizz','buzz'];
93+
94+
// Declare a const array then try to add a value
95+
const const13 = ['foo','bar'];
96+
const13.push("fizz");
97+
console.log("This is const13 after adding the new value ",const13);
98+
99+
// Declare a const array then try to delete a value
100+
const const14 = ['foo','bar'];
101+
const14.pop();
102+
console.log("This is const14 after deleting a foo value ",const14);

ES6/Declarations/let.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Name: let
3+
* Spec: https://www.ecma-international.org/ecma-262/6.0/#sec-let-and-const-declarations
4+
*
5+
*/
6+
7+
8+
// Declare variables but don't assign them a value;
9+
let let1;
10+
var var1;
11+
console.log("The declared but unassigned var1: ",var1, "has a type of: ",typeof(var1));
12+
console.log("The declared but unassigned let1: ",let1, "has a type of: ",typeof(let1));
13+
14+
15+
// Declare at higher scope and use at lower scope
16+
let let2 = 1;
17+
console.log("This is let2 before the if-statement ",let2);
18+
if('a' === 'a'){
19+
console.log("This is let2 within the if-statement ",let2);
20+
}
21+
console.log("This is let2 after the if-statement ",let2);
22+
23+
24+
// Declare at higher scope and modify at lower scope, but use again at original scope unchanged
25+
let let3 = 1;
26+
console.log("This is let3 before the if-statement ",let3);
27+
if('a' === 'a'){
28+
let let3 = 2;
29+
console.log("This is let3 within the if-statement ",let3);
30+
}
31+
console.log("This is let3 after the if-statement ",let3);
32+
33+
34+
// Declare at inner scope and try to use again at outer scope
35+
console.log("This is let4 before the if-statement ",typeof(let4));
36+
if('a' === 'a'){
37+
let let4 = 2;
38+
console.log("This is let4 within the if-statement ",let4);
39+
}
40+
console.log("This is let4 after the if-statement ",typeof(let4));
41+
42+
43+
/*
44+
* ATTENTION / WARNING
45+
* The following 2 examples will only work properly within a browser window (not server-side)
46+
*
47+
*/
48+
49+
50+
// Attempt to use 'this' at global scope
51+
let let5 = 1;
52+
var var2 = 1;
53+
console.log("This is the value of this.let5", typeof(this.let5));
54+
console.log("This is the value of this.var2",this.var2);
55+
56+
// Attempt to use variables on the window opbject
57+
let let6 = 1;
58+
var var3 = 1;
59+
console.log("This is the value of window.let6", typeof(window.let6));
60+
console.log("This is the value of window.var3", window.var3);
61+
62+
63+
/*
64+
* ATTENTION / WARNING
65+
* Some of the following examples should throw an error
66+
*
67+
*/
68+
69+
// Try to re-declare the same variables (from the top of this page), with values
70+
var var1 = 0;
71+
console.log("This is the var1 with a value assigned to it ",var1);
72+
73+
// Expect an error at this point. We can't "catch" this error because the try/catch statement introduces a new block level
74+
let let1 = 0;
75+
76+
// Try to modify a let at the same level it was declared, with keyword
77+
let let7 = 1;
78+
let let7 = 2;
79+
80+
// Try to modify a let at the same level it was declared, omitting keyword
81+
let let8 = 1;
82+
let8 = 2;
83+
console.log("This is the value of let8 now",let8);
84+
85+
// Try to access a let that hasn't been declared yet but is declared below
86+
console.log("This is the type of let9 before we declare it",typeof(let9));
87+
let let9 = 1;

0 commit comments

Comments
 (0)