DEV Community

Keerthana M
Keerthana M

Posted on • Edited on

Basic Array Methods In JS

BASIC ARRAY METHODS:

  • Array length()
  • Array delete()
  • Array concat()
  • Array copyWithin()
  • Array flat()
  • Array slice()
  • Array splice()
  • Array toSpliced()
  • Array isArray()
  • Array toString()
  • Array at()
  • Array join()
  • Array pop()
  • Array push()
  • Array shift()
  • Array unshift()

1. Array Length:

  • The length property returns the length (size) of an array:

    Output: 4

  • The length property can also be used to set the length of an array:

Output:

From the above output, we analysed that. when we have two elements and if we print for an three elements to be printed, the empty space is created.

2. Array toString():

  • The toString() method returns the elements of an array as a comma separated string:
  • Every JavaScript object has a toString() method.

Output:
Banana,Orange,Apple,Mango

3. Array at():

  • Array at() property is used to get an element by its index value.
    Example:

  • Get the third element of fruits using at():

Output:
Apple

4. Array join():

  • The join() method also joins all array elements into a string.
  • It behaves just like toString(), but in addition you can specify the separator:


output:
Banana * Orange * Apple * Mango

5.Popping and Pushing

  • When you work with arrays, it is easy to remove elements and add new elements.
  • This is what popping and pushing is:
  • Popping items out of an array, or pushing items into an array.

1.Array pop():

  • The pop() method removes the last element from an array.
  • The pop() method returns the value that was "popped out".


Output:
Mango
['Banana','Orange','Apple']

2. Array push():

  • The push() method adds a new element to an array (at the end):
  • The push() method returns the new array length:

*Output:
*

5
['Banana', 'Orange', 'Apple', 'Mango', 'Kiwi']

Top comments (0)