let vs const
var:
- hoisted (always declared at top of scope, global if none)
- function scope
let:
- block scope
- not redeclarable
const:
- block scope
- not reassignable
- not redeclarable
Note: Although it may seem like these hold only semantic meaning, using the
appropriate keywords helps the JS engines' compiler to decide on what to optimize.
3.5
2
`const` is a signal that the identifier won't be reassigned.
`let` is a signal that the variable may be reassigned, such as a counter in a
loop, or a value swap in an algorithm.
It also signals that the variable will be used only in the block it's defined
in, which is not always the entire containing function.
Thank you!
2
0
0
0
var:
* hoisted(global scope)
* function scope
let:
* block scope
* reassignable
* not redeclarable
const:
* block scope
* not reassignable
* not redeclarable
Thank you!
0
0
4.29
7
let: //only available inside the scope it's declared, like in "for" loop,
var: //accessed outside the loop "for"
Thank you!
7
0
4.71
6
// var declares a variable, meaning its value will vary.
// const declares a constant, meaning its value will remain
// consistant and not change.
// If your variable changes throughout the program or website,
// declare it using a var statement.
// Otherwise, if its value does not change, declare it using
// a const statement.
const myConst='A const does not change.';
var myVar='A var does change.';
var myVar=2;
Thank you!
6
0
4.3
11
// ⚡ Initialization
var a; //✅
let b; //✅
const c; //❌ const must have an initial value
// ⚡ Re-Assignment
var d = 1;
d = 10; //✅
let e = 2;
e = 20; //✅
const f = 3;
f = 30 //❌ const value is always constant and cannot be reassigned
// ⚡ Re-Declaration
var g = 1;
var g = 10; //✅
let h = 2;
let h = 20; //❌ must use variable name only to reassign, cannot be reassigned
const i = 3;
const i = 30 //❌ cannot be reassigned
// ⚡ Scope [Global, Function or local and block scope]
// Global Scope ==> Any variable that is declared outside any of the function
// Function Scope(var) ==> Can be accessed inside the function it was declared and its child
// Block Scope(let and const) ==> Con be accessed inside curly brackets where it was declared and its child
function myFunction(){
var myName1 = 'Asmita';
let myName2 = 'Dikshit';
const myName3 = 'Sizen';
console.log(myName1);
console.log(myName2);
console.log(myName3);
if(true){
console.log(myName1);
console.log(myName2);
console.log(myName3);
var myAge1 = 13;
let myAge2 = 15;
const myAge3 = 14;
console.log(myAge1);
console.log(myAge2);
console.log(myAge3);
}
console.log(myName1);
console.log(myName2);
console.log(myName3);
console.log(myAge1);
console.log(myAge2);
console.log(myAge3);
};
myFunction();
Thank you!
11
0
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line