xxxxxxxxxx
// Explanation of var, let, and const
/*
var:
- With var, variable scope is function-scoped, meaning it is accessible anywhere within the function.
- Variables declared with var can be reassigned and reassigned within their scope.
*/
var myVar = "Hello";
myVar = "World";
console.log(myVar); // Output: World
/*
let:
- With let, variable scope is block-scoped, meaning it is only accessible within the block it is declared.
- Variables declared with let can be reassigned within their scope.
*/
let myLet = "Hello";
myLet = "World";
console.log(myLet); // Output: World
/*
const:
- With const, variable scope is block-scoped, just like let.
- However, variables declared with const cannot be reassigned.
- Once assigned, the value of a const variable can never be changed.
*/
const myConst = "Hello";
myConst = "World"; // This will cause an error because const variables cannot be reassigned.
xxxxxxxxxx
Const vs Let vs Var
const pi = 3.14
pi = 1
cannot do this becuase with const you cannot change the value
_____________________________________
Let --> is block level
for(let i = 0; i < 3; i++) {
console.log(i) --> it will console here
}
console.log(i) ---> Not here
---------------------------------------
Var is for variables available to the entire function
for(var j = 0; j < 3; j++) {
console.log(j) --> it will console here
}
console.log(j) ---> it will console here
xxxxxxxxxx
| | |
| VAR | LET | CONST
SCOPE | function, global | block scope | block scope
HOISTING | yes | no | no
REASSIGN | yes | yes | no
RE-DECLAIR | yes | no | no
xxxxxxxxxx
// Define, Update and Redefine:
// - You can define, update and redefine VAR
// - You can define and update LET but cannot redefine it
// - You can define CONST but cannot update or redefine it
// Scope:
// - LET & CONST are block scoped - anything within {} - same nesting level only
// - VAR is (so can LET & CONST be) function scoped - can be accessible within that function and cannot be used outside that function
xxxxxxxxxx
Security level of Let, var and const ; Which one you have to use,,,
var = less securty becuase it can be define another time in any scope { including function also },
let = medium security because, let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.
const = high and ultra security because, const variables are unique in whole code
xxxxxxxxxx
var greeter = "hey hi";
function newFunction() {
var hello = "hello";
}
xxxxxxxxxx
let greeting = "say Hi";
let times = 4;
if (times > 3) {
let hello = "say Hello instead";
console.log(hello);// "say Hello instead"
}
console.log(hello) // hello is not defined
xxxxxxxxxx
var greeter = "hey hi";
function newFunction() {
var hello = "hello";
}
xxxxxxxxxx
var greeter = "hey hi";
function newFunction() {
var hello = "hello";
}
xxxxxxxxxx
var greeter = "hey hi";
function newFunction() {
var hello = "hello";
}