+ 1

Const decleration in a loop

How is it possible to use const in a for of loop .. for (const digit of digits){ whatever } Even though the value of digit in this example gets updated everytime the code runs a.k.a every time the next() functions is called inside the iterator object

2nd Sep 2020, 9:10 PM
Ali Kh
Ali Kh - avatar
4 Respostas
+ 1
Got the answer guys nevermind .. it is the block of code that gets created at every loop run, with every run the variable is declared from scratch, every run is a separate block, separate scope " Const has a block scope just like let. Therefore, a new const is created at each iteration of the loop. This const can't be redefined in the iteration itself." Elena
2nd Sep 2020, 9:33 PM
Ali Kh
Ali Kh - avatar
+ 1
Yes. But where you seen gets updated..? It warns error. Can you post it for clarity....
2nd Sep 2020, 9:21 PM
Jayakrishna 🇮🇳
0
Notice that const declarations cannot be updated (reassigned)
2nd Sep 2020, 9:15 PM
Ali Kh
Ali Kh - avatar
0
Yes. That why I asked for code snippet..
2nd Sep 2020, 9:35 PM
Jayakrishna 🇮🇳