React Js
#4 - let vs const vs var - Clear the ES6 concepts and makes simpler path to learn React
ECMA Script 2015, Known as ES6
It First Arrived with Major JavaScript Changes in June 2015 with these -
- let
- const
- arrow function
- JavaScript class
- array find
- Rest parameters
- And more…
var
- Classic Old Way to Declare Variable from JavaScript Start
let
& const
- ECMA Script 2015 New Way to Declare Variable in JavaScript ES6.
Coding Example -
var
var experienceLevel = 'beginner'; // String
var totalScore = 90; // Number
let
let experienceLevel = 'beginner'; // String
let totalScore = 90; // Number
const
Const authUserID = 439590 // Number & never needs to change
Difference Between let
vs var
vs const
var | let | const |
---|---|---|
Function or Globally scoped | Block { } scoped | Block { } scoped |
Re-Declarable | Not Re-Declarable in that scope.Re-Declarable with new one | Not Re-Declarable |
Use Before Declaration is ok but undefined | Use Before Declaration throws error | Use Before Declaration throws error |
Re Assignable | Re Assignable | Not Re-Assignable for Primitive Data Types. Re-assignable for object & Array |
Suggestion – Don’t use in ES6 | Use in ES6 only when needs to re-assign like for loop or something like that | Always recommended at first to use this. If needs to re-assign, then use let |
Example with var
function checkExperienceLevelAndScore(){
console.log('Experience', experienceLevel); // Use Before declaration is also possible
var experienceLevel = 'beginner'; var age = 25;
if(age > 20){
var totalScore = 100;
var experienceLevel = 'Intermediate'; // Re-Declarable is possible and update the main experienceLevel variable
}
console.log('Experience', experienceLevel);
console.log('Score', totalScore); // Access from outside if block scope is possible
for(var i = 0; i < totalScore; i++){
console.log('no: ', i);
}
console.log('no out: ', i); // Access from outside for block scope is also possible, return 100
}
checkExperienceLevelAndScore();
Example with let
function checkExperienceLevelAndScore(){
console.log('Experience', experienceLevel); // Use Before declaration will throws an error
let experienceLevel = 'beginner';
let age = 25;
if(age > 20){
let totalScore = 100;
let experienceLevel = 'Intermediate'; // Create new experienceLevel variable, in this if scope
}
console.log('Experience', experienceLevel); // Output -> beginner. Can not access inner experienceLevel data
console.log('Score', totalScore); // Throws error
for(let i = 0; i < 100; i++){
console.log('no: ', i);
}
console.log('no out: ', i); // Throws Error, Reference Error
}
checkExperienceLevelAndScore();
Example with const
function checkExperienceLevelAndScore(){
const experienceLevel = 'beginner';
const age = 25;
if(age > 20){
const totalScore = 100;
experienceLevel = 'Intermediate'; // Throws Error, Can not rebind
}
const address ={
streetAddress: 'Dhaka',
postalCode: 1208
};
address.postalCode = 1209; // OK and update the postalCode to 1209
console.log('address: ', address); // Return Updated object with postalCode 1209
}
checkExperienceLevelAndScore();
WHY NOT TO USE var
- var can create code unmaintainable and un-stable.
- undefined state is very common in it’s pattern without any warning
- var will not stop me to write wrong code.
- Can not make same variable in another pseudo block
So, Will WE use let
OR const
- We’ll use const by default.
- If need redeclaration, then use let.
- Our pattern will be like we’ll use almost 99% case, we’ll use const.
- If need to re-assign, then create new one, not replacing the old variable.
Performance let
vs var
// WITH let
console.time("let-time")
for(let i = 0; i < 500000; i++){}
console.timeEnd("let-time"); // 2.27490234375 ms
// WITH var
console.time("var-time")
for(var i = 0; i < 500000; i++){}
console.timeEnd("var-time"); // 2.5810546875 ms
JavaScript Hoisting:
var
- Hoisted to the top
- Can Initialize at any time
let
- Hoisted to the top
- Can not initialize at any time.
- Block of Code knows the variable exist, But can not use before initialization.
const
Same concept as let
JavaScript Hoisting Example:
console.log(nameVar); // undefined and no warning
console.log(nameConst); // ReferenceError
console.log(getAgeHostedOntop()); // Is ok,
function getAgeHostedOntop(){
const age = 20;
return age;
}
console.log(getAge()); // Will throw an error
const getAge = function(){
const age = 20;
return age;
}
var nameVar = "Akash";
const nameConst = "Akash";
Project Source Code Day 4 - https://github.com/ManiruzzamanAkash/ReactBasic2Pro/tree/Day4
Video Tutorial -
#3 Enable Additional Plugins for Better React Development - React Basic to Pro Series
React Tutorials - Beginner to Pro - A Complete React Tutorial Series
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development