5.7 Variable Declaration¶
Re-declaration¶
Task 1: Declare the variables a, b, and c with the keywords var, let, const, respectively. And assign them the values of 1, 2, and 3, respectively.
var a = 1;
let b = 2;
const c = 3;
console.log(a, b, c);
The result is: 1 2 3
Task 2: Refer to task 1; re-declare the variables a, and b.
var a = 1;
let b = 2;
const c = 3;
var a;
let b;
console.log(a, b);
The result is: Uncaught SyntaxError: Identifier 'b' has already been declared
Task 3: Refer to task 1; re-declare the variables a, and c.
var a = 1;
let b = 2;
const c = 3;
var a;
const c;
console.log(a, c);
The result is: Uncaught SyntaxError: Identifier 'c' has already been declared
Assignment¶
Task 4: Assign 10 to myVar1.
var myVar1;
myVar1 = 10;
console.log(myVar1);
The result is: 10
Task 5: Assign 20 to myVar2.
let myVar2;
myVar2 = 20;
console.log(myVar2);
The result is: 20
Task 6: Assign 30 to myVar3.
const myVar3;
myVar3 = 30;
console.log(myVar3);
The result is: Uncaught SyntaxError: Missing initializer in const declaration
Re-assignment¶
Task 7: Re-assign 100 to myVar1.
var myVar1 = 10;
myVar1 = 100;
console.log(myVar1);
The result is: 100
Task 8: Re-assign 200 to myVar2.
let myVar2 = 20;
myVar2 = 200;
console.log(myVar2);
The result is: 200
Task 9: Reassign 300 to myVar3.
const myVar3 = 30;
myVar3 = 300;
console.log(myVar3);
The result is: Uncaught TypeError: Assignment to constant variable.
Hoisting¶
Task 10: Access the variable myVar before its declaration.
console.log(myVar);
var myVar = "Hello";
The result is: undefined
Task 11: Access the variable myVar before its declaration.
console.log(myVar);
let myVar = "Hello";
The result is: Uncaught ReferenceError: Cannot access 'myVar' before initialization
Task 12: Access the variable myVar before its declaration.
console.log(myVar);
const myVar = "Hello";
The result is: Uncaught ReferenceError: Cannot access 'myVar' before initialization
Window¶
Task 13: Log the value of window.a in the console.
var a = 1;
console.log(window.a);
The result is: 1
Task 14: Log the value of window.a in the console.
let a = 1;
console.log(window.a);
The result is: undefined
Task 15: Log the value of window.a in the console.
const a = 1;
console.log(window.a);
The result is: undefined
Summary¶
| Difference | var | let | const |
|---|---|---|---|
| Declaration | var a = 1; //a = 1 | let a = 1; // a = 1 | const a = 1; //a = 1 |
| Re-declaration | var a = 1; var a; //a = 1 | let a = 1; let a; //Identifier 'a' has already been declared | const a = 1; const a; //Identifier 'a' has already been declared |
| Assignment | var a; a = 1; // a = 1 | let a; a = 1; // a = 1 | const a; a = 1; //Missing initializer in const declaration |
| Re-assignment | var a = 1; a = 2; // a = 2 | let a = 1; a = 2; // a = 2 | const a = 1; a = 2; //Assignment to constant variable |
| Hoisting | console.log(a); // undefined var a = 1; | console.log(a); // Cannot access 'myVar' before initialization let a = 1; | console.log(a); // Cannot access 'myVar' before initialization const a = 1; |
| window property? | var a = 1; console.log(window.a); // 1 | let a = 1; console.log(window.a); // undefined | const a = 1; console.log(window.a);// undefined |
| Scope | Function Scoped | Block Scoped | Block Scoped |