Array in JS

Array in JS

·

3 min read

JavaScript arrays are a powerful and versatile data structure that allows developers to store and manipulate multiple values in a single variable. In this blog post, we'll take a look at the basics of arrays, including how to create and access them, as well as a selection of commonly used array methods that can be used to perform various operations on the data stored in arrays.

Creating an Array

Creating an array in JavaScript is quite simple. The following is an example of an array that stores a list of names:

let names = ["John", "Jane", "Bob"];

Accessing Array Elements

Array elements can be accessed using their index, which is a zero-based numbering system. For example, the first element of the above array can be accessed as follows:

console.log(names[0]); // "John"

Array Methods

JavaScript provides a number of built-in methods for working with arrays, such as adding, removing, and modifying elements. Here are a few examples of commonly used array methods:

1. push() -

This method adds one or more elements to the end of an array.

let numbers = [1, 2, 3];
numbers.push(4, 5);
console.log(numbers); // [1, 2, 3, 4, 5]

2. pop() -

This method removes the last element of an array and returns it.

let numbers = [1, 2, 3];
let last = numbers.pop();
console.log(numbers); // [1, 2]
console.log(last); // 3

3. shift() -

This method removes the first element of an array and returns it.

let numbers = [1, 2, 3];
let first = numbers.shift();
console.log(numbers); // [2, 3]
console.log(first); // 1

4. unshift() -

This method adds one or more elements to the beginning of an array.

let numbers = [1, 2, 3];
numbers.unshift(4, 5);
console.log(numbers); // [4, 5, 1, 2, 3]

5.slice() -

This method returns a new array that contains a subset of the original array, specified by a start and end index.

let numbers = [1, 2, 3, 4, 5];
let subset = numbers.slice(1, 3);
console.log(numbers); // [1, 2, 3, 4, 5]
console.log(subset); // [2, 3]

6. example of array itteration

how to use a for loop to iterate through and print the elements of an array:

let numbers = [1, 2, 3, 4, 5];
for (let i = 0; i < numbers.length; i++) {
  console.log(numbers[i]);
}

The for loop iterates through the array by using the length property of the array to determine the number of iterations. The i variable is used as the index to access each element of the array. On each iteration, the current element is logged to the console using the console.log() method.

Another way to iterate through an array is using forEach method which applies a function to every element of an array.

let numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(element) {
    console.log(element);
});

or use arrow function

let numbers = [1, 2, 3, 4, 5];
numbers.forEach(element => console.log(element));

Both of these examples will produce the same output in the console:

1
2
3
4
5

You can use any of these methods to iterate through an array and perform some action on each element, such as printing them to the console.