|
| 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); |
0 commit comments