Constant


#1

What is Java Script Constant ?

It confusing statement about constant variable.
Java Script Constant you can not change or add the values but in other end when I use the array I simply can add the new object with .push property.

I would like to know so what is difference between let and constant?


#2

that is true, and with objects you could also add properties. But you can’t re-assign the variable:

const myArray = ['a', 'b', 'c'];
myArray.push('d'); // this goes fine
myArray = {}; // produces error, given the variable is a constant 
// and can't be re-assigned

#3

let is known as a block scope declaration. Try it…

var a = 5;
{
  let a = 42;
}
console.log(a)    // 5

The above demonstrates an anonymous block which has its own scope.

Once we declare a variable with let, we cannot re-declare it (in the same scope). We can give it a new value, though.

Anything with a block can be made to have its own scope which prevents a variable from leaking into its parent scope. This is unlike var which adopts the scope where it is declared by can still be seen outside of that scope (except where the scope belongs to a function).

for (var i = 0; i < 10; i++) {
    //
}
console.log(i)    // 10    =>  known as leakage
i = null;
for (let i = 0; i < 10; i++) {
    //
}
console.log(i)    // undefined    => no leakage

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.