Are you a newbie to javascript? Do you find it difficult to remember concepts of javascript? well, let me share how I remember concepts and differences between elements that look and sound similar π€―.
Difference between Var, Let & Const.
I am going to use the link or story method which connected the differences using an awesome story. Let me know in the comments if you find it easier to understand that usual approach. Let the fun begins!
Linking technical terms to story elements
Have a look at how I have mapped the javascript scopes according to different areas.
π Think of global scope as a map of the world or any country.
π Think of function scope as cities.
π Think of block scope as a home.
see how one area is larger in comparison to others. It helps to memorize it.
Now let's move on to the main characters of this story.
π₯ Introducing to you the variable siblings!
How to remember each scope π€
In JavaScript, users can declare a variable using 3 keywords that are var, let, and const.
Once upon a time, there was a family which had 3 siblings named
var,
let, and
const. Although they had some similarities(used for declaring variables) they were different from each other(different scope)
The scope of the var keyword is the global or function scope.
It means variables defined outside the function can be accessed globally, and variables defined inside a particular function can be accessed within the function.
Var was a traveler, he had access to so many countries and cities(global and function cope) that he has almost covered the whole world. He also had homes in many cities he would visit(if defined in function can be accessed in function).
- Let and Const keywords are block-scoped.
- Block is {} and code in between.
- Variables defined with both can not be re-declared.
- Variables defined with Let can be reassigned.
- Variables defined with Const can't be reassigned.
Let was an introvert like her twin sister Const. They would stay at home and when Var comes home they would not let him be in charge of anything because it's their space(block scope). They were the queens of the house, they always thought of themselves as irreplaceable(can not be re-declared).
If Let doesn't like someone she would give them as many chances as they'd ask(Let can be reassigned), whereas const Const believes in only one chance(Const can't be reassigned).
That's it this is how I used to remember all the things in javascript. You can also try the same and let me know if it helps you in any way. Waiting for your comments!
Thanks for reading, I am not a good writer but I at least triedπ, Should I do similar stories for other topics?
Top comments (0)