Whether the array element is a separate variable?

const example = [1, 2, 3];
In this case, the example is not a new variable (a constant), and the reference to the array.

const item = example[0];
What in this case is item? A new variable (constant) or still a reference to the array element?
April 4th 20 at 13:00
2 answers
April 4th 20 at 13:02
Solution
Hi! There will be a new variable with the value example[0]. It is easy to check :)
const example = [1, 2, 3];
const item = example[0]; // 1
example[0] = 10;
console.log(example); // [10, 2, 3]
console.log(item); // 1
The variable is always the variable, the question is how you have prisvoili, by reference or by value - Nicolas.Huel commented on April 4th 20 at 13:05
@Filomena24 is it possible to change the value of the elements of the array that is set as a constant??? - alayna_Dooley commented on April 4th 20 at 13:08
@America.Torphy39, If you really want, you can :) And actually const in js works a bit differently than in other languages. const says that you can't change very constant, in the case of object(an array object) can change its elements, but not example = 5 - this is a mistake.
Read more about let & const can be read here: https://learn.javascript.ru/let-const - rose_Rogahn commented on April 4th 20 at 13:11
April 4th 20 at 13:04
Better consider a more complex example
<code lang="javascript">
const example = [{val: 1}, {val: 2}, {val: 3}];
const item = example[0]; 
console.log(item)
item.key = 1
console.log(example);
example[0] = {val: 8}
console.log(item)
console.log(example);
</code>

And after googling about this assignment in JavaScript the link and the parameter
And Yes, in comments written correctly, a variable is always a variable, but that's the way it is assigned, by reference or by value is a separate issue.
You mean, in the case of "simple" types(int, string) assigns by value, but if the object of the link? - rose_Rogahn commented on April 4th 20 at 13:07
@Filomena24, it - Nicolas.Huel commented on April 4th 20 at 13:10

Find more questions by tags JavaScript