DEV Community

mohandass
mohandass

Posted on

Array basic methods in j.s

  • In javascript,array methods are built in the functions that help you add,remove,or manipulate elements in the array.

1. Array length

  • The length is property that tells you how many elements are in an array.

EXAMPLE:

  • Inside the array have a 4 elements.So the size of length is 4.

OUTPUT:
4

2. Array toString()

  • The toString() array method that coverets an array an array into a comma separated string.

EXAMPLE:

  • The toString method is join all elements into a string separate
    a comma.

  • this method dose not change the original array.

OUTPUT:
BMW,TATA,BENZ,VOLVO

3. Array at()

  • The at() method is used to access an elements in array using its indexed.

EXAMPLE:

  • The carbrand.at(2) returns an indexed element 2 is the "BENZ"
  • Dose not modify the array.

OUTPUT:
BENZ

4. Array join()

  • The join() method is used to covert an array into a string it's join all elements with a specified separator.

  • It's behaves just like toString().

EXAMPLE:

  • It this join() array method we gives the ( - ).so it's create a array into a string joins with inbetween (-) all elements.

  • The original array stays unchanged.

OUTPUT:
BMW-TATA-BENZ-VOLVO

5. Array pop()

  • The pop() is used to remove the last elements from a array and it's return that elements.

EXAMPLE:

  • This pop() method is remove the array last elements "VOLVO"
  • it's modified the original array.

OUTPUT:
[ 'BMW', 'TATA', 'BENZ' ]

6. Array push()

  • The push() method is used to add one or more elements in end of the array.

EXAMPLE:

  • This push() method add element "AUDI" to the end an array.

  • It is also modified the original array and new length of the array.

OUTPUT:
[ 'BMW', 'TATA', 'BENZ', 'VOLVO', 'AUDI' ]

7. Array shift()

  • The shift() array method is used to remove the first elements from the array and return that all other elements to string index.

EXAMPLE:

  • This shift() method is remove the first element "BMW" and then remaining element are shift into the place of removed element.

  • This method is changed the original array.

OUTPUT:
[ 'TATA', 'BENZ', 'VOLVO' ]

8. Array unshift()

  • The Array unshift() is used to add one or more elements to the starting from the array.

EXAMPLE:

  • This unshift() method is add "MG" elements beginning the array.

  • It's changed the original array and return the new length of the array.

OUTPUT:
[ 'MG', 'BMW', 'TATA', 'BENZ', 'VOLVO' ]

9. Array.isArray()

  • The Array.isArray() method is used to check whatever a value is an array it's return the true and false otherwish it's not a array.

EXAMPLE:

  • If the carbrand is array.so the return is "true".

OUTPUT:
true

10. Array delete()

  • The Array delete() method is used leaves undefined holes in the array.

  • If you the pop() or shift() method instead of delete.

EXAMPLE:

  • Use delete method to remove the mentioned value.But another elements are not shift the array.

  • The array length is not changed.

OUTPUT:
[ 'BMW', <1 empty item>, 'BENZ', 'VOLVO' ]

11. Array concat()

  • The concat() is used to merge two or more array into the new array without changing the original array

EXAMPLE:

  • The concat method merge the carbrand2 array and carbrand1 array in a combine with order for all values

  • The original array dose not changed

OUTPUT:

[ "BMW", "TATA", "BENZ", "VOLVO", "AUDI", "HONDA", "TOYOTA", "KIA" ]

12. copyWithin()

  • The copyWithin() method used to copy the part of array elements to the another location in the same array

EXAMPLE:

OUTPUT:
[ 'BMW', 'TATA', 'BENZ', 'TATA' ]

13. Array flat()

  • A Array flat() method is used create a new array where sub-array elements are marge with main array

EXAMPLE:

OUTPUT:

[ 'BMW', 'TATA', 'BENZ', 'VOLVO' ]

14. Array slice()

  • The slice() method is slice out a piece of array into a new array

EXAMPLE:

  • Return a new array

  • It's not changed the original array

OUTPUT:
[ 'BMW', 'TATA' ]

15. Array splice()

  • The splice() method is used to add,remove,or replace elements in an array

EXAMPLES:

removing elements

Adding elements

Replace elements

16. Array toSpliced()

EXAMPLE:

Top comments (0)