Answers for "const vs var vs let javascript"

26

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

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
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
0

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.
Posted by: Guest on February-12-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
-1

what is the different between var and let and const in js

var tester = "hey hi";
    
    function newFunction() {
        var hello = "hello";
    }
    console.log(hello); // error: hell
o is not defined
Posted by: Guest on July-30-2021

Browse Popular Code Answers by Language