+ 5
Var vs const
what is different
2 Respuestas
+ 5
Mr Maruf thanks for all of this info
+ 4
variable is a named location for storing a value. That way an unpredictable value can be accessed through a predetermined name i.e:
var x = 1;
var x = 1+1;
console.log(x); // output is 2
Constants are block-scoped, much like variables only, the value of a constant cannot change through re-assignment, and it can't be redeclared i.e:
// NOTE: Constants can be declared with uppercase or lowercase, but a common
// convention is to use all-uppercase letters.
// define MY_FAV as a constant value 7
const MY_FAV = 7;
// this will throw an error
MY_FAV = 20;
// will print 7
console.log('my favorite number is: ' + MY_FAV);
// trying to redeclare a constant throws an error
const MY_FAV = 20;
// the name MY_FAV is reserved for constant above, so this will also fail
var MY_FAV = 20;