Recently, I stumbled upon an intriguing concept regarding the behavior of const
when dealing with different data types such as number
, string
, Boolean
, Object
, and arrays
.
const num1 = 10
num1 = 20 //throws error
const arr1 = [1,2,3,4]
arr1.push(5) //works
arr1.pop() //works
arr1[2] = 7 //works
arr1 = [2,3] //throws error
It appears that for array
and objects
, you can change their values but not reassign them while using const
. Does this imply that there is no real advantage in declaring an object
or array
as a const
since you can modify their values at any time?
If someone could shed some light on how exactly const
operates behind the scenes, I would greatly appreciate it!