VAR vs LET vs CONST | JavaScript Variable Redeclare vs Reassign with Practical Examples

  Рет қаралды 2,290

WebStylePress

WebStylePress

2 жыл бұрын

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

Пікірлер: 4
@VHNaresh
@VHNaresh 8 ай бұрын
AMAZING EXPLANATIONS
@MauriceWilliams
@MauriceWilliams Жыл бұрын
Thank you Sir.
@Kuch_Bhi-lets_explore_it
@Kuch_Bhi-lets_explore_it Жыл бұрын
Seriously . the way you explained is amazing one of the best explanation on whole youtube thank You so much 🙌🙌
@zishanahmad4557
@zishanahmad4557 Жыл бұрын
your way of explaining is very good
Differences Between Var, Let, and Const
8:37
Web Dev Simplified
Рет қаралды 185 М.
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 17 МЛН
Double Stacked Pizza @Lionfield @ChefRush
00:33
albert_cancook
Рет қаралды 115 МЛН
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 12 МЛН
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 204 МЛН
JavaScript Let vs Var vs Constant | Mosh
6:51
Programming with Mosh
Рет қаралды 283 М.
The moment we stopped understanding AI [AlexNet]
17:38
Welch Labs
Рет қаралды 843 М.
Why can you modify an object created with const?
5:01
Deeecode The Web
Рет қаралды 674
#38. How to use Var, Let and Const | JavaScript Full Tutorial
8:50
How I use C++: a line-by-line code review
1:40:40
strager
Рет қаралды 245 М.
WTF Is JavaScript Variable Hoisting
8:50
Colt Steele
Рет қаралды 27 М.
Protocol Buffers Crash Course
36:07
Hussein Nasser
Рет қаралды 240 М.
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 17 МЛН