Answers for "let 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
19

var vs let js

let: //only available inside the scope it's declared, like in "for" loop, 
var: //accessed outside the loop "for"
Posted by: Guest on May-22-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
2

difference in var let and const

var: 
	* hoisted(global scope)
    * function scope
let:
    * block scope
    * reassignable
    * not redeclarable
const: 
    * block scope
    * not reassignable
    * not redeclarable
Posted by: Guest on August-10-2021
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
2

var or const in javascript

// 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;
Posted by: Guest on July-09-2020

Browse Popular Code Answers by Language