Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Array Methods in JavaScript | Example Program

Tags: array

An Array in JavaScript is an ordered collection of values that can be of distinct type.

In other words, an array is an object that can hold multiple values at a time. It is used to store multiple values in a single variable.

It becomes really useful when we want to store a large amount of data of the same type. For example, suppose we want to store details of 500 students.

If we use variables, then we will have to create 500 variables that can be very boring and tedious tasks. But, with the help of a single array, we can do the same task easily.

We can access elements of an array by referring to its index number. The index number of the first element in an array is zero.

Array object provides several usable methods that help developers to access array elements easily and efficiently. So, let’s a glance at usable array methods in JavaScript.

Built in Methods of Array Object in JavaScript


The built-in methods inside array object are classified into three categories for ease of understanding. They are:

  • Accessor methods
  • Mutator methods
  • Iterator methods

Let’s understand all of them in detail with examples.

Accessor Methods

Accessor methods in JavaScript are those methods that do not modify the original arrays. They work on a copy of the calling array.

In other words, a copy of array is constructed and the Accessor methods perform its operations on that copy of the array.

All the accessor methods in the array object are defined inside Array.prototype property. JavaScript provides the following accessor methods to work with arrays.


1. concat(): The concat() method joins (or concatenates) two or more arrays. It returns a copy of a new array containing the joined arrays. This method does not change the existing arrays.


2. join(): This method joins all elements in an array into a string and returns string. It does not change original array. A specified separator will separate the elements of an array. The default separator is comma (,). If you omit separator, the array elements will separate with a comma.


3. sort(): This method sorts elements of an array in a specific order. It does not create a new array. It sorts elements of an array in its own place. That is, the sort() method overwrites the original array.

The sort() method sorts the array elements in either alphabetic or numeric, and either ascending (up) or descending (down).


4. reverse(): The reverse() method reverses the order of elements in an array in its own place. It does not create a new array as it overwrites the original array.

The general syntax to define reverse() method is as:

array.reverse();

For example:

Program code 1:


Output:
      Reverse order: Ivaan,Deep,Miller,John
      Reverse order: 57,76,100,86,95

5. includes(): This method checks whether a certain value or element is present in the array or not. If the element is present, it returns true. Else, it returns false.

It is case sensitive in nature. The includes() method comes into two variant flavors, whose general syntax is as:

array.includes(value);
array.includes(value, index);

The first method searches the entire array starting from the beginning if we pass an only value. If we pass an index in the second variant method, the search starts with the index provided.

If the value of index is equal to or greater than the array length, the method returns false. Let’s understand with the help of an example.

Program code 2:



6. indexOf(): This method searches an array for an element with a specified value and returns the index of element where it is first found in the array.

If the specified element is not found in the array, it returns -1. The indexOf() method searches the array from starting to end. This method comes in two variant whose syntax is as:

array.indexOf(element);
array.indexOf(element, index);

In the above syntax, array is the array to be searched. The element and index are the values from where the search starts. The index value is optional. If we do not provide it, the index is taken as zero, by default.

Let’s understand it with the help of an example program.

Program code 3:


Output:
      1
      4
     -1

7. lastIndexOf(): This method returns the last index at which an element is found in an array. It searches an array from end to starting, which represents the first element.

It returns the index if the element found. If the element is not found, it returns -1 to calling function. The general signature of this method is as:

array.lastIndexOf(element, start_index);

In this syntax, start_index represents the position (index) to start the search from. It is an optional parameter. Let us understand with an example program.

Program code 4:


Output:
      4
      2
     -1

8. toString(): This method is commonly used to get the elements of an array in a string. The toString() method converts an array into string value that comprises comma-separated values, similar to the default join() method.

The general signature of this method is as:

array.toString();

Array.prototype overrides the toString() method of Array object. The outcome does not include square brackets or any other delimiter around the array value.

Let’s take an example program based on the toString() method.

Program code 5:


Output:
      Tripti,Ruchi,Deep,Tim,Bob
      10,20,A,B,30

9. slice(): This method extracts a sequence out of an array. It returns a slice, or subarray, of the specified array. It creates a new array that is a copy of original array and works on the copy of that array. The original array stays the same.


In this tutorial, you learned array methods in JavaScript with example programs. Hope that you will have understood the basic concepts of all array methods and practiced all programs. Please, email us if you get incorrect anything in this tutorial.
Thanks for reading!!!

The post Array Methods in JavaScript | Example Program appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Array Methods in JavaScript | Example Program

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×