You won't forget how to use variables in js after reading this!

krishnapankhania

Krishna Pankhania

Posted on March 11, 2022

You won't forget how to use variables in js after reading this!

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.

Linking Scopes to the Story


Now let's move on to the main characters of this story.

πŸ₯ Introducing to you the variable siblings!

Main Characters


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 namedvar,let, andconst. 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?

πŸ’– πŸ’ͺ πŸ™… 🚩
krishnapankhania
Krishna Pankhania

Posted on March 11, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related