1. let
β βletβ is used to declare a block-scoped variable. It means that the variable declared with βletβ can only be accessed within the block in which it was declared.
β βletβ is used to declare a block-scoped variable. It means that the variable declared with βletβ can only be accessed within the block in which it was declared.
2. const
β βconstβ is also used to declare a block-scoped variable, but with the difference that its value cannot be reassigned once it is initialized.
β βconstβ is also used to declare a block-scoped variable, but with the difference that its value cannot be reassigned once it is initialized.
3. var
β βvarβ is used to declare a variable with function-level scoping. It means that the variable declared with βvarβ can be accessed within the function in which it was declared, or even outside the function if it is declared globally.
β βvarβ is used to declare a variable with function-level scoping. It means that the variable declared with βvarβ can be accessed within the function in which it was declared, or even outside the function if it is declared globally.
In this example, βxβ is declared twice, first in the βtest()β function and then within the βifβ block.
The second declaration of βxβ using βvarβ is not creating a new variable with a new scope, bt rather modifying the original variable, which is accessible outside the βifβ block.
The second declaration of βxβ using βvarβ is not creating a new variable with a new scope, bt rather modifying the original variable, which is accessible outside the βifβ block.
Recommendation π‘
Overall, my recommendation to use βletβ and βconstβ instead of βvarβ, especially in modern JavaScript, because βletβ and βconstβ provide block-scoping, which is more predictable and less error-prone.
Overall, my recommendation to use βletβ and βconstβ instead of βvarβ, especially in modern JavaScript, because βletβ and βconstβ provide block-scoping, which is more predictable and less error-prone.
You can get my free JavaScript e-book that has 100+ free resources.
π haiderkh1.gumroad.com
π haiderkh1.gumroad.com
That's a wrap!
If you enjoyed this thread:
1. Follow me @slow_developer for more of these
2. RT the tweet below to share this thread with your audience
If you enjoyed this thread:
1. Follow me @slow_developer for more of these
2. RT the tweet below to share this thread with your audience
Loading suggestions...