Let 1/2
In the very first chapter of the section we learn that there are three ways to declare a variable:
letconstvar
In the later versions of Javascript language known as ES6(ES2015), the let keyword was introduced. It works better than the var keyword. The variables written with the let keyword can also be overwritten.
let has Block Scope:
Variables declared with the let keyword has block scope and cannot be accessed outside that scope, for example.
1234if (2 == 2){ let name = 'David' } console.log(name)
As you can see we get an error that says that the name is not defined.
Note:
Donβt worry about the if and == for now they will be covered in the later chapters.
Swipe to start coding
In the following code, a variable named city with the assigned value of Berlin has been given to you. You are also given an if statement that checks whether 4 is greater than 2 or not (for now, donβt worry about the if). Your task is to declare a variable with the same name as above inside the if with the value of London. Display the variable outside the if.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain the difference between block scope and function scope?
What happens if I use `var` instead of `let` in this example?
Can you give more examples of using `let` in different scopes?
Awesome!
Completion rate improved to 2
Let 1/2
Swipe to show menu
In the very first chapter of the section we learn that there are three ways to declare a variable:
letconstvar
In the later versions of Javascript language known as ES6(ES2015), the let keyword was introduced. It works better than the var keyword. The variables written with the let keyword can also be overwritten.
let has Block Scope:
Variables declared with the let keyword has block scope and cannot be accessed outside that scope, for example.
1234if (2 == 2){ let name = 'David' } console.log(name)
As you can see we get an error that says that the name is not defined.
Note:
Donβt worry about the if and == for now they will be covered in the later chapters.
Swipe to start coding
In the following code, a variable named city with the assigned value of Berlin has been given to you. You are also given an if statement that checks whether 4 is greater than 2 or not (for now, donβt worry about the if). Your task is to declare a variable with the same name as above inside the if with the value of London. Display the variable outside the if.
Solution
Thanks for your feedback!
single