I am reading Nodejs API, and I confused with iteration of buffer:
for (const b of buf10) {
console.log(b)
}
const
is used to declare constants, so why use const
instead of let
?
I am reading Nodejs API, and I confused with iteration of buffer:
for (const b of buf10) {
console.log(b)
}
const
is used to declare constants, so why use const
instead of let
?
Because b
is a constant in the loop scope. Remember that let
and const
have block scope in ES6. Every iteration does create a new variable which will stay constant in its scope.
const is used to declare constants, so why use const instead of let?
Because you can either use var
let
or const
for declaration purpose however they behave differently.
In this case,
for (const b of buf10) {
console.log(b)
}
Works because for each iteration you are getting a new const b and ending after the current iteration.
Concluding that if you know before hand that you are not going to modify the variable inside loop scope, you can go for it safely.
You'll see an error if you try to modify the b
inside the loop.