Arrays
An array is an object that can store multiple values at once.
It can hold many values under a single name
Creating an Array
let students = [“Mary”, “Mercy”, “Milka”, “Mark”];
console.log(students);
Basic Operations of Arrays
Accessing Arrays
● Each element of an array is associated with a number called an index.
● The index specifies the position of the element inside the array.
● We can use an array index to access the elements of the array.
● The index of arrays starts with 0, not 1.
Example
let numbers = [1, 2, 3, 4, 5]
// access first element
console.log(numbers[0]);
// access third element
console.log(numbers[2]);
Accessing Last Element
We can access the last array element using [array.length – 1] index number.
// accessing last element
console.log(students[students.length-1]);
, Modifying the Array Elements
Elements in an array can be modified by assigning a new value to their corresponding index.
students[0] = "Esther";
console.log(students);
Adding Elements to an Array
Elements can be added to the array using methods like push() and unshift().
// Adding elements in an array
// Adds an element at the end of the array
students.push("Melvin");
console.log(students);
// adds an element at the beginning of an array
students.unshift("Tyler");
console.log(students);
Removing Elements in an array
Remove elements using methods like pop(), shift(), or splice().
// Removes last element
students.pop();
console.log(students);
// Removes the first element
students.shift();
console.log(students);
An array is an object that can store multiple values at once.
It can hold many values under a single name
Creating an Array
let students = [“Mary”, “Mercy”, “Milka”, “Mark”];
console.log(students);
Basic Operations of Arrays
Accessing Arrays
● Each element of an array is associated with a number called an index.
● The index specifies the position of the element inside the array.
● We can use an array index to access the elements of the array.
● The index of arrays starts with 0, not 1.
Example
let numbers = [1, 2, 3, 4, 5]
// access first element
console.log(numbers[0]);
// access third element
console.log(numbers[2]);
Accessing Last Element
We can access the last array element using [array.length – 1] index number.
// accessing last element
console.log(students[students.length-1]);
, Modifying the Array Elements
Elements in an array can be modified by assigning a new value to their corresponding index.
students[0] = "Esther";
console.log(students);
Adding Elements to an Array
Elements can be added to the array using methods like push() and unshift().
// Adding elements in an array
// Adds an element at the end of the array
students.push("Melvin");
console.log(students);
// adds an element at the beginning of an array
students.unshift("Tyler");
console.log(students);
Removing Elements in an array
Remove elements using methods like pop(), shift(), or splice().
// Removes last element
students.pop();
console.log(students);
// Removes the first element
students.shift();
console.log(students);