Why can methods such as splice () push () modify constants declared by const?

< H2 > take a chestnut: < / H2 >
const arr = ["","",""]
arr.splice(2,1)
console.log(arr)
output: ["eat", "sleep"]
< H2 > another chestnut: < / H2 >
const arr = ["","",""]
arr.push("")
console.log(arr)
output: ["eat", "sleep", "drink water", "pan him"]
< H2 > and this modification will result in an error: < / H2 >
const arr = ["","",""]
arr = ["","","",""]
console.log(arr)
output: error
< hr > < H1 > Why is this? < / H1 >
May.25,2022

http://es6.ruanyifeng.com/-sharpdo...

What

const actually guarantees is not that the value of the variable cannot be changed, but that the data stored in the memory address that the variable points to shall not be changed. For simple types of data (numeric, string, Boolean), the value is stored at the memory address that the variable points to, so it is equivalent to a constant. But for the compound type of data (mainly objects and arrays), the memory address that the variable points to is only a pointer to the actual data. Const can only guarantee that the pointer is fixed (that is, it always points to another fixed address). As for whether the data structure it points to is variable, it is completely out of control.


The
const declaration creates a read-only reference to a value. However, this does not mean that the value it holds is immutable, but the variable identifier cannot be reassigned. For example, in the case where the reference content is an object, this means that you can change the content of the object (for example, its parameters).

const


because when const declares, what cannot be changed is the pointer of this variable in memory. Arrays and objects are reference types. Splice and push do not change the address reference of the array in memory, so they do not report an error

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b326fe-2be2e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b326fe-2be2e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?