In computer programming, a variable or scalar is a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value or in easy terms...
WIKIPEDIA
If the above Wikipedia definition of a variable seems a tad bit too confusing – do not despair. Variables, simply explained – is just a way to grab a piece of memory and assign it some value. And even better, we can also reference that memory location by the name that we give our own variable!
This gives us infinite possibilities to create whatever variables we want. (referencing simple data, mathematical notations, or real-life objects).
How about JavaScript and variables?
Now, in JavaScript there are some syntaxic differences between the different forms of variable declaration. For example:
var firstName = "John"; var age; let earthRadius = 6371; let earthRadiusUnit = "km";
The keyword var
simply declares a variable. The let
keyword is a bit more restrictive as it limits the access to the variable to only the block scope (basically any block with curly braces). To see this in action change the variable declaration in the if
block below to let
instead of var
.
One of these will throw an error – can you guess which one?
if(true) { var earthRadius = 6317; var earthRadiusUnit = "km"; } console.log("The radius of the earth is " + earthRadius + earthRadiusUnit); *************************************************************************************** if(true) { let earthRadius = 6317; let earthRadiusUnit = "km"; } console.log("The radius of the earth is " + earthRadius + earthRadiusUnit);
Accessing the variables in the second block shouldn’t be possible because they are block-scoped to the if
. This is useful for hiding and limiting the visibility of your variables, which prevents overwriting and additional confusion.
Another way we can declare variables is const
. It is similar to let
, because it is only block-scoped, but it additionally forces the developer to give the value immediately (well, to be honest – it can’t force you to do anything, but it will throw a nasty error). Also – we can’t reassign const
variables.
const marsRadius; // will throw "Uncaught SyntaxError: Missing initializer in const declaration" const marsRadius = '3,389.5 km'; marsRadius = '6,371 km'; // will throw "Uncaught TypeError: Assignment to constant variable"
It doesn’t mean that we can’t change const
variables, it just means that we cant set new values to them. We still can internally change a referenced value (an array or an object).
const earthData = { radius: '6,371 km', population: '7.674 billion' } earthData.countries = 195; //this works!
The most basic lesson is that we should only use const for primitive declarations – which will ease the number of re-assignment and declaration errors inside our programs.
Final Words
These are variables, and as you can see they don’t seem that hard, right? Of course, we didn’t go into a lot of detail, but knowing the way we can use these different types can be useful (and is commonly asked in interviews!).
For more articles please click below, or check the blog.
- Largest Palindrome Product
- Largest Prime Factor
- Even Fibonacci Numbers
- Multiples of 3 or 5
- How to find the missing number in a given integer array of 1 to 100?
- Understanding Javascript prototypes
- Difference between Promise.all() and Promise.race()
- JavaScript generators
- Using this and arguments
- Arrow functions in JavaScript