DEV Community

Discussion on: JavaScript: Introduction to Scope (function scope, block scope)

Collapse
 
siddh profile image
Siddh

If we are not able to reassign the value in the variable that is defined with the const
then why it is changing his value every time, when the loop gets executed

const array = [1,2,3,4] ;

for (let index = 0; index < array.length; index++) {
const element = array[index];
console.log(element);
}

answer is

1
2
3
4

Collapse
 
devhamzaa profile image
Dev Hamza

you are storing the array[index] in element
you are not re-declaring but you are updating the value of element