Re:JavaScript fundamentals before learning React
by josephg on Friday July 20, 2018 @07:52AM
(#17574225)
> The reference could be immutable but allow value reassignment, but is not the implementation of const.
You can reassign values through a const reference in javascript. For example:
const x = [1]
x[0] = 2 // ok!
This just doesn't work with primitive values, because primitive values are immutable and copy-by-value. So this works with lists but not strings (because strings are a primitive and thus immutable).Or, put in C++ friendly terms, javascript doesn't distinguish between pointer reassignment and value reassignment. It only has reassignment, which is illegal for const variables. Variables which hold non-primitives (lists, objects, etc) are actually pointers to those values. const does not affect the mutability of the value stored at the pointer.
Wouldn't this definition suggest that the value CAN be reassigned? The reference could be immutable but allow value reassignment, but is not the implementation of const.