Table of Contents
👉 Difference between Let and Const
👉 Store User Input in a variable with JS
👉 Sorting Numbers in JS
What does Let and Const stand for?
Let’s first talk about what Let and Const actually mean. Let stands for “letting” and Const stands for “Constant“. That alone should already give you a hint if it doesn’t let me break it down for you with an example.
Which obviously translates to:
wonderfulCode language: Bash (bash)
Pretty logical, or isn’t it?
Const, on the other hand, explains itself pretty well. You use Const, or Constant for that matter, for variables that do not change. Let me give you another example.
Can you guess what the output of this is? If you guessed false, you are right.
We first assign
isSnowing the boolean value of true, and then re-assign it the value false a line below. The ultimate result is false.
Now, what happens if we refactor this code to use Const instead of Let?
We receive an error message:
If we go ahead and comment out
isSnowing = false, our code will run:
Why Const and Let were implemented?
If you have asked yourself this question by now, I got the answer to you: Readability.
To understand the difference between let and const, you have to simply think about the readability of code. If somebody else has to work on your code and sees a Const variable, he immediately knows that this is a fixed value and it is not going to change later on in the code, making it much easier for him or her to understand your code, whereas the opposite is true for Let.
Once I dug a little deeper it made perfect sense to me as well.