JavaScript Array Methods

JavaScript Array, properties and methods

Posted by

JavaScript Array is a special variable that can store more than one value at a time. The values can be numeric, character, string, etc.

Syntax:

Example:

There are also built-in properties and methods in JavaScript that you can use as needed.

  1. Array Length
  2. Array Join()
  3. Forach()
  4. Sort()
  5. Reverse()
  6. Find() and FindIndex()
  7. Include()
  8. Every() And Some()
  9. Push() And Pop()
  10. Unshift() And Shift()
  11. Map()
  12. Filter()
  13. Reduce()

Array Length

The length of the array returns a numeric value.

Syntax:

Example:

Array Join()

We use array join to convert an array into a string in JavaScript. We use a specific separator to separate strings.

Syntax:

Let’s take example:

If we do not add any separator then it will add default comma(,).

If you pass the separator then it separates the value by that separator.

Array forEach()

We use array forEach() to apply an operation on array elements.

There are three syntax for the array forEach().

Here are three writing styles of the code of forEach in JavaScript but all three will give the same output.

Output:

Sort()

It is used to sort an array. By default, the array will be sorted in ascending order.

Syntax:

We use the following method to sort an array:

  1. a-b = positive which means the element b will sort before a.
  2. a-b = negative which means the element a will sort before b.
  3. a-b = zero which means the element will stay in the same position.

Let’s take examples to understand:

Sort the array from lowest to highest order:

Sort the array from highest to lowest order:

Reverse()

It is used to reverse the order of an array. The array on which the reverse() is applied, the original array changes.

Syntax:

Example:

  • const numbers = [1,2,3,4]
  • numbers.reverse() => [4,3,2,1]

Find() and FindIndex()

Returns the first element or index that passes the testing function.

Syntax: Both array methods use same syntax.

Find(): Returns the first element that passes the testing function. If no match then returns undefine.

findIndex(): Returns the first index of element that passes the testing function. If no match then returns -1.

Example of find():

Example of findIndex():

Include()

It returns a boolean to indicate if an array includes a specific value.

Syntax:

Note: Value is a case sensitive.

Example:

if you pass index value to 1 then it will start checking the value from 1 index and it will return “false” because apple is available on zero index.

Example:

Value is a case sensitive if you pass “Apple” then it will return “false”.

Every() And Some()

Both JavaScript method returns boolean based on a function.

every(): Returns a boolean to indicate if every element in the array matches the criteria provided.

some(): Returns a boolean to indicate if an array contains some elements that match the criteria provided.

Syntax:

Example of every() JavaScript function: In the below example, all the elements are greater than 20 so it will return true.

In the below example, all elements of the array are not starting with ‘a’ so it will return false.

Example of some() JavaScript function: In the below example, checking if some elements are greater than 50. If yes then returns true otherwise it will return false.

Lets take other Example: In the below example, checking the some name of fruits are starting with ‘a’ if yes then return true otherwise return false.

Push() And Pop() function in JavaScript

Both of these array methods manipulates the length and contents of the array by adding and removing the data to and from the end of an array.

Push():

It is used to add the content to the end of an array.

Syntax:

It returns length property of the newly changed array.

Example: In the below example, one more fruit has been added at the end of the array using push method of the JavaScript.

Pop():

It is used to remove the content from end of the array.

Syntax:

It returns length property of the newly changed array.

Example:

Unshift() And Shift() function in JavaScript

Both of these array methods manipulate the length and contents of an array by adding and removing data to and from the beginning of an array.

Unshift():

It is used to add the content at the beginning of an array.

Syntax:

Example:

Shift():

It is used to remove the content from the beginning of an array.

Syntax:

Example:

Map() and FlatMap() method in JavaScript Array

map():

It creates a new array based on the function applied to each element in the array you are interacting over.

Syntax:

Example: In the below example, 5 will be added in each elements of the array.

Example: You can make a new array for showing the full name.

flatMap():

The flatMap() method uses a mapping function to map each element in an array and then flattens the results into a new array.

Syntax:

Example: flatMap() method has splitted the each letter in the below code.

Filter() method in JavaScript Array

It creates a new array based on whether or not elements pass the test provided by the function provided.

Syntax:

Example: In below example, getting an array which obtains marks more than 50.

Let’s take another example: In the below example, we will get an array which have more than 9000 salary.

Output of above example:

Reduce() Function in JavaScript

It executes a reducer function against each element in an array and returns a single value. The returned value is the accumulated value.

Syntax:

  • accumulator – It is required and it is accumulated or total.
  • currentValue – Required.
  • index – Optional.
  • array – Option.
  • initialValue – Optional.

Example: Below example it gives total sum of the value of the array.

Conclusion

You have checked the JavaScript array and methods. Many interviewer asks the questions about the methods map, filter, push and pop, unshift and shift, every and some, find and findIndex etc.

You can check the more JavaScript topic:

Declaring JavaScript Variables: Var, Let and Const
Cookies, Local Storage and Session Storage in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *