Array methods in javascript

Arrays are the most powerful and useful data type in javascript and it has many methods this blog consist of all the possible method in a very precise

Array methods in javascript

1.What is Array ?

Array are generally described as "list-like objects"; An array is a collection of similar data elements stored at contiguous memory locations. It is the simplest data structure where each data element can be accessed directly by only using its index number.

2. Methods in Array

Array method.jpeg

1. arr.at(index)

arr.at(index) : Used to find element at any index

Parameter : The array element's return value's index (position). When a negative index is given, the element returned will be discovered by counting back from the end of the array.

Returns : the array's element matching the specified index. If the specified index cannot be found, returns undefined.

Example :

const stringArray = ["India", "Canada", "France", "Germany", "Russia"];

const ans = stringArray.at(2);
console.log(ans); // Expected Output->   France

output:

France

2. arr.length

arr.length : Used to find length of the array.

Returns : The length property of an array returns the number of elements. The following example shows how to use the length property:

Example :

const stringArray = ["India", "Canada", "France", "Germany", "Russia"];

const ans = stringArray.length;
console.log(ans); // Expected Output->   5

Output:

5

3. arr.push(parameter)

arr.push(parameter) :Used to add element or elements in the last position of the array. It changes the actual array.

Parameter : You can give one or more elements at a time. When passing multiple values make sure they are comma separated.

Example :

const stringArray = ["India", "Canada", "France", "Germany", "Russia"];

stringArray.push("USA");
console.log(stringArray); // Expected Output->  ["India", "Canada", "France", "Germany", "Russia", "USA"]

output:

 ["India", "Canada", "France", "Germany", "Russia", "USA"]

4. arr.unshift(parameter)

arr.unshift(parameter) : Used to add element or elements in the first position of the array. It changes the actual array. It is just opposite of push rather it put elements at the very first of array.

Parameter : You can give one or more elements at a time. When passing multiple values make sure they are comma separated.

Example :

const stringArray = ["India", "Canada", "France", "Germany", "Russia"];

stringArray.unshift("USA", "Iceland");
console.log(stringArray); // Expected Output->  [ "USA", "Iceland", "India", "Canada", "France", "Germany", "Russia"]

output :

[ "USA", "Iceland", "India", "Canada", "France", "Germany", "Russia"]

5. arr.pop()

arr.pop() : Used to remove an element from the end of an array. It also changes the array. After this method the array removes its last element.

Returns : Last element from the array.

Example :

const stringArray = ["India", "Canada", "France", "Germany", "Russia"];

const ans = stringArray.pop();
console.log(ans); // Expected Output->  Russia

output :

Russia

6. arr.shift()

arr.shift() : Used to remove an element from the start of an array. It also changes the array. After this method the array removes its first element.

Returns : First element from the array.

Example :

const stringArray = ["India", "Canada", "France", "Germany", "Russia"];

const ans = stringArray.shift();
console.log(ans); // Expected Output->  India

output :

India

7. arr.indexOf(parameter)

arr.indexOf(parameter): To find the index of an element, you use the indexOf() method:

Returns : Index of the element in the array if it doesn't exists it will return -1.

Example :

const stringArray = ["India", "Canada", "France", "Germany", "Russia"];

const ans = stringArray.indexOf("Canada");
console.log(ans); // Expected Output->  1

output :

1

8. arr.concat(Array)

arr.concat(Array) : Used to concat two arrays into one.

Parameter : Array

Returns : After concatenation of the two arrays it gives single array with both the values of the array.

Example :

const stringArray = ["India", "Canada", "France", "Germany", "Russia"];
const numArray = [1, 2, 3, 4, 5, 6, 7, 8];

const ans = stringArray.concat(numArray);
console.log(ans); // Expected Output-> ["India", "Canada", "France", "Germany", "Russia", 1, 2, 3, 4, 5, 6, 7, 8];

output :

["India", "Canada", "France", "Germany", "Russia", 1, 2, 3, 4, 5, 6, 7, 8];

9. arr.every(callbackFunc)

arr.every(callbackFunc) : Used to iterate over every element and find the certain specified condition in true or false.

Parameter : function which checks the condition for all the elements in the array

Returns : True or False if the condition is true for all elements in the array then it returns true otherwise it will return false.

Example :

const stringArray = ["India", "Canada", "France", "Germany", "Russia"];
const ans = stringArray.every((element) => element.length > 3);
console.log(ans); // Expected Output-> true

Output :

true;

10. arr.fill(value)

arr.fill(value) : Used to fill the array with that value. (Note all elements in the array will be this exact value.)

Parameter :

  • arr.fill(value):It will fill the array with that value.

  • arr.fill(value, start) : It will also fill the array with that value but from where you specified the start value.

  • arr.fill(value, start, end) : It will also fill the array with that value but from where you specified the first value till the end value (but end value is exclusive).

Returns : The modified array, filled with value.

Example :

const stringArray = ["India", "Canada", "France", "Germany", "Russia"];

const ans = stringArray.fill("India");
console.log(ans); // Expected Output-> [ 'India', 'India', 'India', 'India', 'India' ]

Output :

["India", "India", "India", "India", "India"];

Did you find this article valuable?

Support priyanka chaudhari by becoming a sponsor. Any amount is appreciated!