5.6 Let¶
Let Declaration¶
Task 1: Declare a variable called num, and set its value to 10.
var num = 10;
console.log(num);
The result is: 10
Task 2: Re-do task 1, and re-declare num with the let keyword.
let num = 10;
console.log(num);
The result is: 10
Task 3: Refer to task 2; assign the value of 20 to the num variable.
let num = 10;
num = 20;
console.log(num);
The result is: 20
Task 4: Declare name variable with the let keyword. Assign "Ali" to it.
let name;
name = "Ali";
console.log(name);
The result is: Ali
Task 5: Declare type variable with the let keyword. Assign true to it.
let type;
type = true;
console.log(type);
The result is: true
1- Let
Variables declared with Let can be reassigned.
Task 6: Re-declare the variable myVar with the var keyword. Assign "hello" to it.
var myVar = "Bye";
var myVar = "Hello";
console.log(myVar);
The result is: Hello
Task 7: Re-declare the variable myVar with let keyword. Assign "hello" to it.
let myVar = "Bye";
let myVar = "Hello";
console.log(myVar);
The result is: Uncaught SyntaxError: Identifier 'myVar' has already been declared
Task 8: Re-declare the variable secret with let keyword. Assign "Top Secret" to it.
let secret = "Not a secret";
let secret = "Top Secret";
console.log(secret);
The result is: Uncaught SyntaxError: Identifier 'secret' has already been declared
2- Let
Variables declared with Let can not be re-declared.
Let Scope¶
Task 9: Log the value of myVar outside the function.
function myFunc(){
var myVar = 5;
return myVar;
}
console.log(myVar);
The result is: Uncaught ReferenceError: myVar is not defined
Task 10: Log the value of myVar outside the function.
function myFunc(){
let myVar = 5;
return myVar;
}
console.log(myVar);
The result is: Uncaught ReferenceError: myVar is not defined
Task 11: Log the value of myVar outside the curly brackets.
{
var myVar = "I am inside a block";
}
console.log(myVar);
The result is: I am inside a block
Task 12: Log the value of myVar outside the curly brackets.
{
let myVar = "I am inside a block";
}
console.log(myVar);
The result is: Uncaught ReferenceError: myVar is not defined
Task 13: Log the value of myBlockVar variable outside the if statement.
if(true){
var myBlockVar = true;
}
console.log(myBlockVar);
The result is: true
Task 14: Log the value of myBlockVar variable outside the if statement.
if(true){
let myBlockVar = true;
}
console.log(myBlockVar);
The result is: Uncaught ReferenceError: myBlockVar is not defined
3- Let
The let variables are block-scoped.
Let & Window¶
Task 15: Declare abc variable with the var keyword. Set the value of abc to 1. Log window.abc in the console.
var abc;
abc = 1;
console.log(window.abc);
The result is: 1
Task 16: Declare abc variable with the let keyword. Set the value of abc to 1. Log window.abc in the console.
let abc;
abc = 1;
console.log(window.abc);
The result is: undefined
Task 17: Declare greeting variable with the var keyword. Set the value of greeting to "Hello everyone". Log window.greeting in the console.
var greeting;
greeting = "Hello everyone";
console.log(window.greeting);
The result is: Hello everyone
Task 18: Declare greeting variable with the let keyword. Set the value of greeting to "Hello everyone". Log window.greeting in the console.
let greeting;
greeting = "Hello everyone";
console.log(window.greeting);
The result is: undefined
4- Let
let does not create properties of the window object when declared globally.
Let Initialization¶
Task 19: Access the variable myVar before it is declared.
console.log(myVar);
var myVar = 5;
The result is: undefined
Task 20: Access the variable myVar before it is declared.
console.log(myVar);
let myVar = 5;
The result is: Uncaught ReferenceError: Cannot access 'myVar' before initialization
5- Let
let variables are initialized in the execution phase.