Const variables in a for loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Const variables in a for loop

Hello Everyone... Can someone explain this piece of code please..... for (let i=0;i<10;i++){ const z =i console.log (z) } Why aren't we getting any errors?... Aren't we declaring a const again?

26th Sep 2019, 7:33 AM
Amirabbas
Amirabbas - avatar
2 Answers
+ 2
const variables are block-scoped so the lifetime of z ends at the end of every iteration, allowing const z to be redeclared in the next iteration.
26th Sep 2019, 8:38 AM
jtrh
jtrh - avatar
0
jtrh thanks i got it 🌹
26th Sep 2019, 11:20 AM
Amirabbas
Amirabbas - avatar