DEV Community

Sivakumar Mathiyalagan
Sivakumar Mathiyalagan

Posted on

Basic Methods in Array

  1. Array.length This Method is used to find the length of the given array,its return type is number

const days = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]

let size = days.length // output : 7

2.Array.toString()
This Method returns the entire array as a coma seperated string, return type string

const days = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]

let size = days.toString() // output :sunday,monday,tuesday,wednesday,thursday,friday,saturday

3.Array at()
This Method returns the value of the specified index inside the braces,
return type depends on the value on that index

const days = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]

let day = days.at(2) // output : tuesday

4.Array join()
This Method joins the array with specified character inside the braces, return type is string

const days = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]

let week = days.join("-") // output : sunday-monday-tuesday-wednesday-thursday-friday-saturday

5.Array pop()
This Method is used to pop the last element from the array

const days = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]

let week = days.pop() // output : sunday,monday,tuesday,wednesday,thursday,friday

6.Array push()
This method adds a new element to an array at the end

const days = ["sunday","monday","tuesday","wednesday","thursday","friday"]
let week = days.push("saturday") // output : sunday,monday,tuesday,wednesday,thursday,friday,saturday

7.Array shift()
This method removes the first array element and "shifts" all other elements to a lower index

const days = ["sunday","monday","tuesday","wednesday","thursday","friday"]
let week = days.shift() // output : monday,tuesday,wednesday,thursday,friday,saturday

8.Array unshift()
This method adds a new element to an array (at the beginning), and "unshifts" older elements

const days = ["monday","tuesday","wednesday","thursday","friday"]
let week = days.unshift("sunday") // output : sunday,monday,tuesday,wednesday,thursday,friday,saturday

9.Array.isArray()
This Method checks if the variable given inside its braces is actually an array or not, return type boolean

const days = ["monday","tuesday","wednesday","thursday","friday"]
let check = Array.isArray(days) // output : true

10.Array delete()
This Method deletes the given index inside the array but leave its position undefined, its is not highly recommended

const days = ["monday","tuesday","wednesday","thursday","friday"]
delete days[2];
console.log(days); // output ['monday', 'tuesday', empty, 'thursday', 'friday']

11.Array concat()
This method creates a new array by merging (concatenating) existing arrays

const days = ["monday","tuesday","wednesday"]
const nextDays = ["thursday","friday"]

const week = nextDays.concat(days);
output : thursday,friday,monday,tuesday,wednesday

12.Array copyWithin()
method copies array elements to another position in an array

const days = ["monday","tuesday","wednesday","thursday","friday"]
let week = days.copyWithin(2,0)
output :'monday', 'tuesday', 'monday', 'tuesday', 'wednesday'

13.Array flat()
This method creates a new array with sub-array elements concatenated to a specified depth
const days = [["monday","tuesday"],["wednesday","thursday"],"friday"]
let week = days.flat(); output : ["monday","tuesday","wednesday","thursday","friday"]

14.Array slice()
This method slices out a piece of an array into a new array by the given index

const days = ["monday","tuesday","wednesday","thursday","friday"]
let week = days.slice(2);// output : 'wednesday', 'thursday', 'friday'

15.Array splice()
This method can be used to add new items to an array at the given index

const days = ["monday","tuesday","wednesday","thursday","friday"]
days.splice(5,0,"saturday")// output : 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'

16.Array toSpliced()
The difference between the new toSpliced() method and the old splice() method is that the new method creates a new array, keeping the original array unchanged, while the old method altered the original array

const days = ["monday","tuesday","wednesday","thursday","friday"]
let week = days.toSpliced(0,1); output : 'tuesday', 'wednesday', 'thursday', 'friday'

Top comments (0)