Рет қаралды 2,290
In this video we will look at JavaScript errors like, SyntaxError: Identifier has already been declared. TypeError: Assignment to constant variable. Can not assign because it is a constant. In JavaScript declaring variables and assigning value to variables is like this type keyword of var, let or const and then identifier name. To assign it a value we will type identifier, equal sign and then value.
We have cleared difference between declaring a variable in JavaScript, redeclaring this variable, assign it a value, reassign it another value and then declaring a variable and assign it a value at the same time. Variable declarations are hoisted and assignments don't. In JavaScript, when it comes to, declaring a variable, assigning it a value, it's easily understandable. But when it comes to understanding variable redeclaration and reassignment, it becomes a little bit confusing.
Because we can declare variables in JavaScript by using var, let and const keywords. Variable redeclaration and reassigmnet depend on how they have been declared and by using which keyword. So we will talk about redeclaring and reassigning variables in JavaScript while we use var, let and const. VAR, LET and CONST are JavaScript keywords to declare variables. Here are the rules:
- These keywords are used in lowercase.
- var can be redeclared and reassigned.
- let can not be redeclared but can be reassigned
- const can not be redeclared, and can not be reassigned
When we create a variable, we declare it with the keywords var, let, or const and assign it a value.
- Can var be redeclared?
We declared a variable using var keyword and called it n.
If we redeclare this variable with same name and keyword.
It will not complain.
So var can be redeclared in JavaScript.
- Can let be redeclared?
If we declare a variable using let keyword and then redeclare it again. It will not work. It will throw error of 'Identifier has already been declared'. So let can not be redeclared in JavaScript.
- Can const be redeclared?
If we declare a variable using const keyword and then redeclare it. It will not work. It will throw error of 'Identifier has already been declared'. So const can not be redeclared in JavaScript. So var can be redeclared but let and const can not be redeclared.
- What about reassigning variables in JavaScript?
Reassign means we are giving the variable another value.
- Can var be reassigned?
Var can be reassigned without any issue.
- Can let be reassigned?
So let can be reassigned also without any issue.
- Can const be reassigned?
const can not be reassigned. It will throw error of 'TypeError: Assignment to constant variable.'
So basically use const where value is something like an API URL or where value is not suppose to be changed. For example we use map Array method on a variable declared by using const. Map method will not change original value. It will give you a new value. Original value will be unchanged.
In such cases using const is best.
Use let where original value will be modified. Such as in case of loops. And for var. Try to avoid var as much as possible.
const can not be redeclared and reassigned. That does not mean it can not be changed at all.
If we take an array declared by using const. We can push items in it. We can also remove items from it and more. So do not get confused when we say const can not be reassigned. Here we are not reassigning const value. We are pushing items to already assigned value.
In more tutorials we also have talked about variable scope, variable hoisting, global scope, function scope, block scope in JavaScript.
Thank You!
👍 LIKE VIDEO
👊 SUBSCRIBE
🔔 PRESS BELL ICON
✍️ COMMENT
Channel: / webstylepress
Website: www.webstylepress.com
FaceBook: / webstylepress
Twitter: / webstylepress
GitHub: github.com/webstylepress
#JavaScript #JS #var #let #const #JavascriptTutorial #JavaScriptVariables #WebStylePress