Array helps us to hold different kind of values all together in a single place. The values will be inside the square bracket [ ]
. It can be numbers, boolean, string much more.
Syntax:
const a = [ val1, val2, val3 ];
// Normal way of storing values
const country1 = "Swiss"
const country2 = "France"
// Array
const countries = ["swiss", "UK", "France"];
We can access elements by using their index values and it always starts at index of 0
console.log(countries[0]); // swiss
console.log(countries[countries.length - 1]); // France -> { 3 - 1 => 2 }
In the above example the total length of an array is 3 and when we do countries.length - 1
the output will be france
cause it have an index of 2.
We can mutate the element ie: changing the values of elements through their index values.
countries[1] = "venice";
console.log(countries); // UK is replaced by Venice