We all at some point in time got confused about using the appropriate keywords in redeclaring a variable. If you are or have been in this situation, congratulations!! because you are following in the same footsteps even the best JavaScript programmers of all-time followed.
Before you go on to understand how to redeclare a variable at different instances, you are supposed to know how to declare a variable.
Here are amazing website links to get you started on Declaring Variables.
Congrats on learning about the declaration of variables!
Redeclaring Variables using the const
keyword
The meaning of the word 'Redeclaration' could be tricky for beginners. To redeclare a variable simply means to declare an already declared variable or identifier regardless of whether in the same block or outer scope.
You could declare and initialize a variable using the const
keyword.
//declared and initialized the variable(firstName) and assigned a value of 'Patrick'.
const firstName = 'Patrick';
console.log(firstName)
// Patrick
You may have tried to redeclare the variable or even try to change its value still using the const
keyword.
const firstName = 'Patrick';
const firstName = 'John';
//Uncaught SyntaxError: Identifier 'firstName' has already been declared.
it signals a syntax error because const
never allows us to tamper with its identifier. With these experiments, we could
agree that A variable declared using the 'const' keyword cannot be redeclared and its value cannot be reassigned.
Redeclaring Variables using the let
keyword
You can declare and initialize a variable using the let
keyword.
//declared and initialized the variable and assigned a value of 'Okafor'.
let lastName= 'Okafor';
console.log(lastName) // Okafor
Let's try to redeclare the identifier and change its value.
let lastName = 'Okafor';
let lastName = 'Okafor';
//Uncaught SyntaxError: Identifier 'lastName' has already been declared
It logs out an error signalling that the identifier has already been declared therefore we must use another identifier. Now with these experiments, you can see also that;
A variable that is declared using the 'let' keyword cannot be redeclared again and its value cannot be changed or reassigned.
Note: We could declare a variable once and refer to it again without using the let keyword
. For example
let lastName = 'Okafor';
lastName = 'Doe';
console.log(lastName) // Doe
This can run successfully because we declared with the let
keyword. The const
keyword would output an error.
const lastName = 'Okafor';
lastName = 'Doe';
console.log(lastName) // null
Redeclaring Variables using the var
keyword
Just like using the const
and let
keywords, You can declare and initialize a variable using the var
keyword.
//declared and initialized the variable and assigned a value of 'Cat'.
var myPet= 'Cat';
console.log(myPet) // Cat
Let's try to redeclare the variable and also change its value. We start with redeclaring the variable
var myPet = 'Cat';
var myPet = 'Cat'; // Cat
This time around, it doesn't display any error. It logs out the value. Now, let's change the value;
var mypet= 'Cat';
var myPet = 'Dog'; // Dog
With the experiments using the var
keyword to redeclare variables, we could see that the output was the redeclared value. It overrides the declared variable, in our terms, it \" redeclared\" a variable. Therefore we say
A variable that is declared with the 'var' keyword can be redeclared and its value could be reassigned'.
Note: To learn more about redeclaring variables, it's necessary to know the basics of JavaScript scope.. It's a whole topic on its own but I tried to explain redeclaring variables involving Scopes of JavaScript Here.
Now, do more practice.