Table of contents
Introduction
Hello all I am here with another blog In this, I am gonna share how scoping allows the programmer to access data from the variable. As I am super familiar with python programming language I faced a lot of confusion while learning JS variable concepts, So to make things more clear I am attempting to explain the most basic concept in javascript
Before jumping into the concept directly I just want to make a small recap about the variables, and declaration of those things like that. So if you are familiar with a few programming languages like c, c++, and java there is a concept like the "Declaration Of Variables" it is nothing but, We have to declare the type of the variable before using it in the code base. Meanwhile, JavaScript also needs to declare the variables but in quite a different and simple way !!!
Types Of Variable Declarations
By convention javascript support FOUR types of declarations, But when it comes to usage we mostly work with only TWO.
As both the let and var variables explanation is too vast I am dividing the blog into parts, Do try to stay tuned !!!!!!
The Ways Are
using var (var a = 10)
using let (let a =10)
using const (const a = 10)
without any keyword (a =10)
So, let's see about the let
Let
It is one of the variable declaration keywords used to declare the variable that can store any data type. But they have their own rules about their declaration, their scope and all these things, So the rules, up to which extent they can be accessed can be deeply explained below.
- Variables assigned with let cannot be redeclared. I will try to explain this with code
let A = 10
console.log(A)// 10
//if we try to assign 20 to the varible A
let A = 20
console.log(A)//SyntaxError: Identifier 'A' has already been declared
- Variables declared with let don't allow "hoisting". Simply, it can be defined as we are trying to access data from the variables without declaring the variable and value.
console.log(A)
let A = 10 // Cannot access 'a' before initialization
If we try to do the same thing using the var variable it does not show any error, but it reminds us by printing an "undefined" statement on the console
3. "Block-scoped" in simple words we can say it as (if, else if and else) blocks means if we declare the variables using let in these blocks their scope is only in that blocks.
let A = 10
if (A ==10){
let A =11
console.log(A)// 11
}
console.log(A)// 10
From the above example, we can say that redeclaring a variable with let in the blocks treats that as an entirely new declaration even when the name of the variables seems to be the same.
4.When we declare a variable using let to initialize the variable within the loopings statements the value of the variable does not change
let A = 10
for (let A=0; A<3; A++){
console.log("hello")
console.log(A)
/* hello
hello
hello
10 /*
Mostly these are the things about the let keyword.
Hurrah !!!! we completed learning one of the most used declaration keywords in javascript. Try to give a pat on your back.
I will catch you on the next blog with the var keyword declaration, stay tuned!!!!
Thank you for the time, Do try to comment rather than likes because it helps to improve myself more feel free to comment.