JavaScript Array Functions Cheat Sheet Part 4

 JavaScript Array Functions Cheat Sheet Part 4



reverse()

reverse() array function reverses an array

Syntax


    arr.reverse()

Example


    [ 123 ].reverse() 
    // [ 3, 2, 1 ]


shift()

shift() removes the first element from an array and returns that element

Syntax


    arr.shift()


Example


    const arr = [123]
    arr.shift()
    // returns: 1 
    // arr is [ 2, 3 ]


slice()

slice() returns a copy of part of the array, while the original array is not modified


Syntax


    arr.slice([begin[, end]])


Example


    [ 1234 ].slice(13
    // [ 2, 3 ]


some()

some() tests whether at least one element in the array passes the test

Syntax


    arr.some(callback(
        element[, index[, array]]
    )[, thisArg])

Example


    [ 1234 ].some(val => val > 3)
     // true


sort()

sort() sorts the elements of array in place

Syntax


     arr.sort([compareFunction])

Example


     [ 1234 ].sort((ab=> b - a)
     // [ 4, 3, 2, 1 ]

splice()

splice() changes contents of an array by removing, replacing and/or adding elements

Syntax


    let arrDeletedItems = array.splice(
        start[, deleteCount[, item1[, item2[,
            ...]]]])

Example


    const arr = [1234]
    arr.splice(12'a'
    // returns [ 2, 3 ] 
    // arr is [ 1, "a", 4 ]

toLocaleString()

toLocaleString() elements are converted to Strings using toLocaleString and are separated by locale-specific String (eg. “,”)

Syntax


    arr.toLocaleString([locales[, options]]);

Example


    [1.1'a'new Date()].toLocaleString('EN'
    // "1.1,a,1/2/2021, 2:03:28 AM"


toString()

toString() returns a string representing the specified array

Syntax


    arr.toString()


Example

   
    [ 'a'23 ].toString()
    // "a,2,3"


unshift()

unshift() adds one or more elements to the beginning of the array and returns the new length

Syntax


    arr.unshift(
        element1[, ...[, elementN]]
    )

Example


    const arr = [123]
    arr.unshift(099)
    // returns 5 
    // arr is [ 0, 99, 1, 2, 3 ]

values()

values() returns Array Iterator object that contains values for each index in array

Syntax


    arr.values()

Example


    ['a''b''c']
    .values() // Array Iterator {  }
    .next() // { value: "a", done: false }
    .value // "a"


Thanks for reading... 👏👏👏


JavaScript Array Functions Cheat Sheet Part 3

Reactions

Post a Comment

0 Comments