I can’t get my head around the usage of the square brackets. I know I am just not understanding it properly, but the lesson doesn’t give any further info and I can’t find answers to my issue anywhere.
var array = [50,60,70];
var data = array;
The variable called “array” has the array [50, 60, 70]. Then under that the code calls array. Why doesn’t that reassign the array to be the value stored in index 0? How can you assign another value from “array” if it has been written over?
My only thought is that square brackets mean something different when there is only one item in the array. But i can’t understand why that would be the case.
All the best,
Challenge: Access Array Data with Indexes
Link to the challenge:
I think the code mentions array just to show that the index of the first array item is zero. The common usage for brackets are:
// Changing value of array item
array = 100;
// then array becomes [100, 60, 70];
// Assigning array item value to variable/constant
var num = array;
// then num equals 60
array accesses the first element in the array.
But the code on its own doesn’t really make much sense because it’s not being used for anything. It would make more sense if it had an assignment or a
var array = [50, 60, 70];
var firstElement = array;
console.log(firstElement); // 50
var secondElement = array;
console.log(secondElement ); // 60
That makes so much more sense now. thank you. I understood that it was doing something to array but I just couldn’t understand what.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.