In most case let and const are almost same, the only difference I known, const cannot be updated but let can.
Let use this example for demonstration
constcolors=['purple','black','yellow','aqua'];// constfor(constcolorofcolors){color='white';console.log(color);// Uncaught TypeError: Assignment to constant variable.}// Letfor(letcolorofcolors){color='white';console.log(color);// white white white white}
I use let only when i want to modify variable's value.
In below example you can't use const because the value of variable is modified by increment one until the condition is false.
for(leti=1;i<3;i++){console.log(i);// 1 and 2}
In below example, the constant variable(random) hold each values of that array.
for(constrandomof['a','b',1,2]){// Here it print values that held by constant random without modify itconsole.log(random);// a b 1 2}
Remember, to be constant does not mean to hold only single data. const can also hold list of data. Good example is array.
// Here constant colors is holding two colorconstcolors=['purple','black'];// First color is purpleconsole.log(colors[0]);// 'purple'// Second color is blackconsole.log(colors[1]);// 'black'
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In most case let and const are almost same, the only difference I known, const cannot be updated but let can.
Let use this example for demonstration
Then their aren't same anymore...
Yes i agree with you on that, but i think thatβs a miss use of const because inside the loop the variable color is changing at each iteration.
When use const in for of loop, in each iteration you get a new variable, which is scoped only to that iteration.
I use let only when i want to modify variable's value.
In below example you can't use const because the value of variable is modified by increment one until the condition is false.
In below example, the constant variable(random) hold each values of that array.
Remember, to be constant does not mean to hold only single data. const can also hold list of data. Good example is array.