Array in Javascript

·

4 min read

Array 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 manner.

We will consider

// for number array
const numArray = [1, 2, 3, 4, 5, 6, 7, 8];

// for string array
const stringArray = ["India", "Canada", "France", "Germany", "Russia"];

Different methods for Array

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.

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

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

Output

France;

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:

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

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

Output

5;

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.

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"];

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.

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"];

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

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

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

Output

Russia;

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

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

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

Output

India;

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.

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

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

Output

1;

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.

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];

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.

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

const ans = stringArray.every((element) => element.length > 3);
console.log(ans); // Expected Output-> true

Output

true;

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.

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 Shubham by becoming a sponsor. Any amount is appreciated!