Lecture Notes Of Day 8
Arrays in JavaScript
What is
an Array in JavaScript?
An array in JavaScript is
a special variable that can hold multiple values, even values of different
types. Arrays allow you to store lists of data and are essential for organizing
and working with data collections.
Example:
let fruits = ['Apple', 'Banana', 'Cherry'];
In this example, fruits is an
array that contains three elements: 'Apple', 'Banana', and 'Cherry'. Arrays are
indexed starting from 0.
Creating
Arrays
Arrays can be created in several
ways:
1. Using
Array Literal Syntax: This is the most common way to create an array.
let fruits = ['Apple', 'Banana', 'Cherry'];
2. Using the
Array Constructor: You can also create an array using the Array()
constructor.
let fruits = new Array('Apple', 'Banana', 'Cherry');
3. Creating an Empty Array: You can create an empty array by leaving the brackets empty.
let
fruits = [];
4. Creating
an Array with a Specific Length: If you know the number of
elements you want, you can initialize an array with a specific length.
let numbers = new Array(5); // Creates an array with 5 empty slots
Accessing
Array Elements
Each item in an array has an
index, starting at 0 for the first element. You can access an array element by
referring to its index.
Example:
let fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits[0]);
// Output: Apple
console.log(fruits[1]);
// Output: Banana
In the above example, fruits[0]
accesses the first element, 'Apple', and fruits[1] accesses 'Banana'.
Array
Length:
To find the number of elements in
an array, use the length property.
let fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits.length);
// Output: 3
Array
Methods
JavaScript provides several
built-in methods to work with arrays. Below are some commonly used array
methods:
1. push()
let fruits = ['Apple', 'Banana'];
fruits.push('Cherry');
console.log(fruits);
// Output: ['Apple', 'Banana', 'Cherry']
2. pop()
let fruits = ['Apple', 'Banana', 'Cherry'];
let
lastFruit = fruits.pop();
console.log(lastFruit);
// Output: Cherry
console.log(fruits); // Output: ['Apple', 'Banana']
3. shift()
let fruits = ['Apple', 'Banana', 'Cherry'];
let
firstFruit = fruits.shift();
console.log(firstFruit);
// Output: Apple
console.log(fruits); // Output: ['Banana', 'Cherry']
4. unshift()
let fruits = ['Banana', 'Cherry'];
fruits.unshift('Apple');
console.log(fruits);
// Output: ['Apple', 'Banana', 'Cherry']
5. splice()
o
start: Index at which to start changing the array.
o
deleteCount: The number of items to remove.
o
item1, item2, ...: Items to add to the array.
Example:
let fruits = ['Apple', 'Banana', 'Cherry'];
fruits.splice(1,
1, 'Orange', 'Mango'); // Removes 1 element at index 1, adds 'Orange' and
'Mango'
console.log(fruits);
// Output: ['Apple', 'Orange', 'Mango', 'Cherry']
6. slice()
o
start: Index at which to start extraction
(inclusive).
o
end: Index at which to end extraction (exclusive).
Example:
let fruits = ['Apple', 'Banana', 'Cherry', 'Date'];
let
slicedFruits = fruits.slice(1, 3);
console.log(slicedFruits);
// Output: ['Banana', 'Cherry']
console.log(fruits); // Output: ['Apple', 'Banana', 'Cherry',
'Date']
Iterating
Over Arrays Using Loops
You can iterate over an array
using loops to perform actions on each element. JavaScript offers several ways
to loop through arrays:
1. Using a for loop
let fruits = ['Apple', 'Banana', 'Cherry'];
for (let
i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
//
Output:
// Apple
// Banana
// Cherry
2. Using a forEach() loop
let fruits = ['Apple', 'Banana', 'Cherry'];
fruits.forEach(function(fruit)
{
console.log(fruit);
});
//
Output:
// Apple
// Banana
// Cherry
3. Using for...of loop
let fruits = ['Apple', 'Banana', 'Cherry'];
for (let
fruit of fruits) {
console.log(fruit);
}
//
Output:
// Apple
// Banana
// Cherry
Summary
In this lesson, we have learned
the basics of arrays in JavaScript:
1. Creating
Arrays: Using array literals or the Array constructor.
2. Accessing
Array Elements: Using indices to access array values.
3. Array
Methods: Common methods like push(), pop(), shift(), unshift(), splice(), and slice()
to modify arrays.
4. Iterating
Over Arrays: Using loops like for, forEach(), and for...of to
iterate through arrays.
Understanding arrays and their
methods is fundamental for managing collections of data and performing
operations on them in JavaScript.
