Answers for "let vs var vs const"

19

var vs 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.
Posted by: Guest on May-11-2021
5

difference between let, var and const in java script

//let is used to declare the variable or initialize a variable..
//let variable can be updated in future.
//Its block scope and we cannot redeclare it.

let a; // Initializing the variable.
let b = 4; // Initializing and declaring variable.
let b = 5; //Error:- cannot redeclare a value but we can update the value.

//var is just like let but var is used to declare a variable value at top of
//the program, its hoisted.

var a; // Initializing the variable.
var b = 4; // Initializing and declaring variable.
var b = 5; //b will be redeclared and we can update its value.

//const variable should be declared and intialized at same time.
//const variable cannot be updated in future.
//we cannot even redeclare the variable in future.

const a = 10; //Initilizing and declaring a variable at a time.
const b; //Error:- const should be declared and intialized.
const a = 11 // Error:- cannot redeclare the variable again.
Posted by: Guest on September-15-2021
14

var let const javascript

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
Posted by: Guest on November-07-2019
5

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.
Posted by: Guest on June-29-2020
8

let vs const

`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.
Posted by: Guest on August-17-2020
0

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();
Posted by: Guest on July-26-2021

Browse Popular Code Answers by Language