DEV Community

Cover image for Array Methods in Javascript
Satya Samaikya Venna
Satya Samaikya Venna

Posted on

Array Methods in Javascript

Introduction to JavaScript Arrays:

  • Arrays store multiple values in a single variable.
  • Can hold different data types.
  • Indexed starting from 0.
  • Ex: array=[1,2,3,4,5]
  • Creating an empty array.Ex: arr=[];
  • Creating an array and initialise it.

Adding or Removing Items:

  • push(): Adds an element to the end.
  • pop(): Removes the last element.
  • shift(): Removes the first element.
  • unshift(): Adds an element to the beginning.
  • length: Returns the number of elements.

Image description


Iteration Methods:

  • ForEach(): Executes a function for each element.

Image description

  • map(): Creates a new array by applying a function.

  • Reduce():Accumulates values into a single element.

Searching and Filtering:

  • filter():Returns all matching elements.
    Image description

  • find():Returns first element that satisfies the condition.

Image description

Extracting / Copying:

  • Slice(): Extracts a portion of an array.

Image description

Updated Section: Modifying and merging arrays

  • splice() – Add, Remove, or Replace (Mutates Original)

Image description

  • concat() – Merge Arrays (Does NOT Mutate Original)

Image description

Conclusion:

  • Enhance efficiency and readability to the code.
  • Useful for adding,removing, searching,iterating and transforming data.
  • JavaScript provides powerful array methods.

Top comments (0)