I came across this interesting concept where const
behave differently for number
, string
, Boolean
vs Object
, 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
For array
& objects
it allows change in value but throws error in assignment. Is it mean, there is no advantage of declaring object
or array
as const
, as you can change all values anytime?
If anyone can explain, I would like to know, how, behind the scene, const
works?