let numbers = [1, 2, 3, 4, 5]; var array = ["hello", 9, true, [1, 2, 3]]; const array2 = ;
To wrap our head around how array indexes work, let's look at the second array. The second array contains four elements. Since we are dealing with computers we always start counting with zero, so the first element in the array, at the 0 index, is the string, "hello." The element in the fourth position, with the index of 3, is another array of integers. You can access the values of the individual indexes with square bracket notation like this:
var array = ["hello", 9, true, [1, 2, 3]]; console.log(array); // [1,2,3] console.log(array); // "hello"
You can also change the value of an array index using bracket notation like this:
var array = ["hello", 9, true, [1, 2, 3]]; array = 10; // console.log(array); // ["hello", 10, true, [1, 2, 3]]
let numbers = new Array(1, 2, 3, 4, 5); // console.log(numbers); // [1, 2, 3, 4, 5]
I prefer to create arrays the other way, but it's ultimately up to you.
var array = ["hello", 9, true, [1, 2, 3]]; console.log(array.length); // 4
The ability to know the length of an array is useful in things like for loops and what not, but it really comes in handy when dealing with arrays where you don't initially know what the size is. For example, like when you are fetching data from an external API.