Arrays in Javascript

Arrays in Javascript

Β·

5 min read

What is an Array? πŸ‘‡

An array is a collection of data stored at contiguous memory locations.Arrays are addressed by using indexes.Index is nothing but its positions.Using Index we are retrieving data elements.

Syntax:

const array_name = [item1, item2, ...];

Array Properties

1.The length Property

The length property in an Array returns the number of elements in that array.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
let length = fruits.length;

output
4

Array Methods

1. at()

The at() method takes an integer value and returns the item at that index.Also allows integers positive and negative .Please Note Negative integers count back from the last item in the array.

let array1 = [2, 4, 6, 8, 10];
let index = 2;
console.log(`Using an index of ${index} the item returned is ${array1.at(index)}`);
index = -2;
console.log(`Using an index of ${index} item returned is ${array1.at(index)}`);

output
> "Using an index of 2 the item returned is 6"
> "Using an index of -2 item returned is 8"

2.concat()

The concat() method is used to merge two or more arrays into one array. Without changing the existing arrays,it returns us a new array.


let letterset1 = ['a', 'b', 'c'];
let letterset2= ['d', 'e', 'f'];
let letterset3= letterset1 .concat(letterset2);
console.log(letterset3);

output
["a", "b", "c", "d", "e", "f"]

3.copyWithin()

The copyWithin() method copies array elements to another position in the array.The copyWithin() method overwrites the existing values.Also it returns without modifying its length. array.copyWithin(target, start, end)

let array1 = ['a', 'b', 'c', 'd', 'e'];
console.log(array1.copyWithin(0, 3, 4));
console.log(array1.copyWithin(1, 3));

output
> Array ["d", "b", "c", "d", "e"]
> Array ["d", "d", "e", "d", "e"]

4.indexOf()

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

let kitchen= ['Carrot', 'Beatroot', 'Potato', 'Pumpkin', 'Tomato'];
console.log(kitchen.indexOf('Beatroot'));
console.log(kitchen.indexOf('Onion'));

output
> 1
> -1

5.join()

The join() method creates and returns a new string by concatenating all of the elements in an array ,separated by commas or a specified separator string mentioned by us.The default is comma (,).

let fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.join());
console.log(fruits .join('-'));

output
> Banana,Orange,Apple,Mango
> Banana-Orange-Apple-Mango

6.pop()

The pop() method removes the last element of an array and returns that element. This method changes the length of the array.

let flowers= ['rose', 'lilly', 'hibuscus', 'daliya', 'jasmine'];
console.log(flowers.pop());

output
> ['rose', 'lilly', 'hibuscus', 'daliya']

7.push()

The push() method adds one or more elements to the end of the array and returns the new length of the array.

let animals = ['pigs', 'goats', 'sheep'];
let count = animals.push('cows');
console.log(count);
console.log(animals);

output
> 4
> ['pigs', 'goats', 'sheep','cows']

8.reverse()

The reverse() method reverses an array.The first array element becomes the last, and the last array element becomes the first.In other words we can say that we are changing the direction opposite to the previously stated

let numset1= ['one', 'two', 'three'];
console.log(numset1);
let reversedset = numset1.reverse();
console.log(reversed);

output
> ["one", "two", "three"]
> ["three", "two", "one"]

9.slice()

The slice() method returns selected elements in an array, as a new array from start to end. The start and end represent the index of items in that array. Please note original array will not be modified.

Syntax : array.slice(start, end)

let fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
console.log(fruits.slice(1, 3));
console.log(fruits.slice(2, -1));

output
> Orange,Lemon
> Lemon,Apple

10.shift()

The shift() method removes the first element from an array and returns that removed element. Actually this method changes the length of the array.

let array1 = [2, 4, 6];
let firstElement = array1.shift();
console.log(array1);
console.log(firstElement);

output
> [4,6]
> 2

11.lastIndexOf

The findLastIndex() method returns the index of the last element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

let fruits = ['Apple', 'Orange', 'Apple']
console.log( fruits.indexOf('Apple') ); 
console.log(fruits.lastIndexOf('Apple') );

output
> 0
> 2

12.map()

The map() method creates a new array but does not change the original array. Syntax : arr.map(function(item, index, array)

let array1 = [1, 4, 9, 16];
let map1 = array1.map(x => x * 2);
console.log(map1);

output
> [2, 8, 18, 32]

13.sort()

The sort() method sorts the elements of an array.The default sort order is ascending.

let months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);

output
> ["Dec", "Feb", "Jan", "March"]

14.find()

The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

let array1 = [5, 12, 8, 130, 44];
let found = array1.find(element => element > 10);
console.log(found);

output
> 12

15.every()

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

let isBelowThreshold = (currentValue) => currentValue < 40;
let array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));

output
> true

16.forEach()

The forEach() method executes a provided function once for each array element.

let array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));

output
> "a"
> "b"
> "c"

πŸ‘‰πŸ”Please go through and Read this short notes. It will help you to understand the commonly used Array methods.

Β