Answers for "differnce between var and let and const js"

7

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
0

difference between var, let, const

// var is a function scope ***
if(true){
    var varVariable = 'This is var';
    var varVariable = 'This is var again';
}

console.log(varVariable); // This is var again

// let is a block scope ***
if(true){
    let letVariable = 'This is let';
    let letVariable = 'This is let again';

    // let variable can't re-define but we can re-assign value


    console.log(letVariable); // let letVariable = 'This is let again';^SyntaxError: Identifier 'letVariable' has already been declared
}

console.log(letVariable); //ReferenceError: letVariable is not defined



// const variable can't re-define and re-assign value
// const is a block scope ***
if(true){
    const constVarible = {
        name: 'JavaScript',
        age: '25 years',
    };
    constVarible.name = 'JS';

    console.log(constVarible) // {name: 'JS',age: '25 years'} <= we can update const variable declared object 
}
Posted by: Guest on September-13-2021

Code answers related to "differnce between var and let and const js"

Browse Popular Code Answers by Language