what is the difference between let and const in javascript
The difference is that with const you can only only assign a value to a variable
once, but with let it allows you to reassign after it has been assigned.
what is the difference between let and const in javascript
The difference is that with const you can only only assign a value to a variable
once, but with let it allows you to reassign after it has been assigned.
let vs var vs const
// ⚡ 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();
difference between var let and const in javascript with example
//functional scope
var a; // declaration
a=10; // initialization;
//global scope
// re-initialization possible
let a;//only blocked scope & re-initialization possible
a=10;
let a =20;
if(true){
let b =30;
}
console.log(b); // b is not defined
const // const also blocked scope,Re-initialization and re-declaration not possible
const a; // throws error {when we declaring the value we should assign the value.
const a =20;
if(true){
const b =30;
}
console.log(b); // b is not defined
console.log(a); // no output here because code execution break at leve b.
difference between var and let and const in javascript
var variable1 // declared using var
const variable2 // declared using const
myFunction1();
function myFunction1() {
variable1 = "hello!";
console.log(variable1);
// "hello!"
}
myFunction2();
function myFunction2() {
variable2 = "hello!";
// error: variable2 is a constant and can not be redifined
}
myFunction3(true);
myFunction3(codition) {
if(condition) {
let variable3 = "helo!" // declared using const
}
variable3 = "hello!";
// error: variable3 is out of scope
}
/*
Therefore,
var:
-can be declared outside any function to be used inside any function
-can be declared inside any function or any other {} which are of only if or if-else or switch etc. and can be used anywhere inside the function
-can be changed again and again anywhere
let:
-can be declared outside any function to be used inside any function
-if declared inside any function or any other {} which are of only if or if-else or switch etc. and can't be used anywhere inside the function and can be only used inside statement
- can be changed again and again only inside the statement in which they are made in
const:
-can be declared outside any function to be used inside any function
-can be declared inside any function or any other {} which are of only if or if-else or switch etc. and can be used anywhere inside the function
-cannot be changed again and agan anywhere, if tried to, will result in an error
*/
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us