Sobes.tech
Middle+

const a = {} a[1] = 'test' for ( ... )

sobes.tech AI

Answer from AI

In this code, a constant a is declared as an empty object:

const a = {}

Then, a property is assigned:

a[1] = 'test'

Although a is declared with const, this does not prevent modifying the contents of the object. const guarantees that the reference to the object a will not change, but the properties of the object can be modified.

Example:

const a = {}
a[1] = 'test'
console.log(a[1]) // 'test'

If you want to iterate over the properties of object a in a loop, you can use for...in:

for (const key in a) {
  console.log(key, a[key])
}

Or get the keys using Object.keys:

Object.keys(a).forEach(key => {
  console.log(key, a[key])
})

Thus, const does not make an object immutable; it only prevents reassignment of the variable.

const a = {} a[1] = 'test' for ( ... ) — Frontend - sobes.tech