Confused about array index lesson

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];
array[0];
var data = array[1];

The variable called “array” has the array [50, 60, 70]. Then under that the code calls array[0]. 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,

Anton

Challenge: Access Array Data with Indexes

Link to the challenge:

I think the code mentions array[0] 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[0] = 100;
// then array becomes [100, 60, 70];

// Assigning array item value to variable/constant
var num = array[1];
// then num equals 60

The line array[0] 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 console.log.

var array = [50, 60, 70];

var firstElement = array[0];
console.log(firstElement); // 50

var secondElement = array[1];
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.