Basic and Interesting method of Array in JavaScript.

Md. Tahsin Amin
3 min readNov 3, 2020

In any programming language, Array is a common and useful tool.

JavaScript Array

In JavaScript, an array is a single variable that is used to store different elements. It is often used when we want to store a list of elements and access them by a single variable. In one sentence, Array is a collection of a group of the same or different data types as like primitive data types(number, string, boolean), structural data types(object, array).

Declaration of an Array:

Initialization of an Array :

We can also update after initialization :

An array in JavaScript can hold different elements:

We can store Numbers, Strings, and Boolean in a single array.

Accessing Array Elements :

Array in JavaScript are indexed from 0 so we can access array elements as follows:

The length property of an Array :

The length property returns the number of elements present in the array. The length of an Array is always one more than the highest index of an Array.

The array has lots of interesting methods, as like that (sort, push, pop, splice, join, etc);

The most interesting method I describe here.

  1. push(): push() insert one or more elements to the end of an array.

2. pop(): remove the last element from an array and return the last element.

3.unshift(): insert one or more elements to the beginning of an array. Example : arrayName.unshift(value)

4.shift(): remove the first element from an array and returns that removed the element.Example : arrayName.shift().

5. indexOf() : method returns the index of the element present in an array. We know index rage [0,array.length-1].If the element not present in array,it returns -1. Example : arrayName.indexOf(value);

6.findIndex(): The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

7.sort(): This method returns sorted array.Number array going to ascending order. String array going to dictionary order.

8. reverse(): This method returns a reverse array. The first element goes to last and the last element goes to first.

9. forEach(): for loop and forEach similar kind of thing, the difference between for and forEach, for is a loop, forEach is a method. forEach executes a provided function once for each array element.

10.join(): This method creates and returns a string using all of the elements in an array, separated by commas or a specified separator string.

11.isArray() : isArray() method determines whether the passed value is an array. Example : Array.isArray(arrayName);

12. concat(): concat() method is used to merge two or more array.This method does not change the existing arrays but instead returns a new array.

13. slice() : This method returns subarray.A subarray is a contiguous part of an array. A shallow copy of a portion of an array into a new array object selected from start to end (end not included). The original array will not be modified. Example: arrayName(start:end)

some point for the slice() method :

  1. end not included.
  2. if you only start this method return [start - array.length];
  3. you can’t use the only end value. example arrayName.slice(,end) // error
  4. you can also use negative number . use negative and see the magic.

14. Splice(): If want to replace any subarray and add a new element.JavaScript has an interesting splice(). This method changes the contents of an array by removing or replacing existing elements and/or adding new elements. You can also use two or more parameters.

Remove element,splice(startIndex,deleteCount(how many you want to delete ))./splice(startIndex,deleteCount, New Item add);

Happy coding.

--

--