DEV Community

Cover image for Arrays in JavaScript
Byali Macqueline
Byali Macqueline

Posted on

Arrays in JavaScript

Array methods

Here, we have some array methods such as ;

1.push(): Used to insert values into an existing array, though the value will be added at the end 9f the array.

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

The output here would be: [1, 2, 3, 4]

2.slice(): Used to get a given range of values from an array at their specific indexes.

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

The range here is from 1 to 4, thus the last value at index 4 is not going to be among the values in the output.

The output here would be;
[1, 2, 3]

3.splice(): Inserts values at specific indexes accordingly as aligned by the user.

Forexample;
let fruits =['apple' ,'mango','orange','peer'];
fruits . splice(2,0,'pawpaw', 'strawberries');
console.log (fruits);

Here, the 2-From where i will start.This means at index 2, we insert the first value(pawpaw) into the array, then ,0-how many we shall replace or delete. Therefore,0 means after insertion at 2, we insert strawberries at index 0, hence remove 0 items next to pawpaw.

The output here would be;

['apple' ,'mango','pawpaw', 'strawberries','orange','peer']

Incase we used 1 instead of 0, we would remove orange hence output would be;

'apple','mango','pawpaw','strawberries','peer'];

4.concatenation(); This can be used to join or add 2 or more arrays to form a single array.

Forexample;

let arr1=[1,2,3,4];
let arr2=[5,6 ,7];
console.log (arr1.concat(arr2))

Output:[1,2,3,4,5,6,7]

Let arr3=[8,9]
Console.log (arr1.concat(arr2,arr3))

Output here would be:[1,2,3,4,5, 6, 7, 8, 9]

5.fill(): This helps us to add a value into the array in a given range.

let arr4 = [1,2,3,4];
arr4.fill('Anurag', 2, 4);
console.log(arr4);

Here Output would be: [1,'Anurag' 4]

6.shift(): This shifts the first item of an array and removes it.

Forexample;

let arr4 = [1,2,3,4];
arr4.shift();
console.log (arr4);

Output here would be: [2,3,4]

7.indexOf(): This helps us to find the index at which a specific value is.

Forexample;

let arr4 = [1,2,3,4];
console.log (arr4.indexOf(3));

Output here would be 2 as the value 3 is stored at index 2.

8.lastIndexOf(): This outputs the index of the last value in an array.

Forexample;

let arr4 = [1,2,3,4];
console.log (arr4.lastIndexOf());

9.include(): This method is used to determine whether it is true or false a particular value is stored at that specific index.

Forexample;

let arr4 = [1,2,3,4];
arr4.include(4,2);

Output here would be false since value 4 is not stored at index 2.

10.pop(): Used to remove a value from the end of the array.

Forexample;

let arr4 = [1,2,3,4];
arr4.pop();
console.log(arr4);

Output here would be; [1,2,3]

11.join(): You can use any string or anything to join or add the values in an array together.

Forexample;

let arr4 = [1,2,3];
console.log (arr4.join('and'));

Output here would be; 1 and 2 and 3.

13.unshift(): This adds elements to the beginning of an array and returns the new length of the array.

My Github link
My Linked in

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more