Array Methods in JavaScript

Array Methods in JavaScript

Array

In JavaScript, an array is an object. It enables us to store a collection of multiple items under a single variable name of any data type and have different methods to perform common operations on an array. A pair of square brackets [ ] can be used to symbolize an array in JavaScript. The array's elements are all separated by commas (,). You can create an array with elements of data type String, Boolean, Number, or Objects.

Syntax

const array_name = [itemA, itemB, itemC,.............];

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst", "FindCoder"];
console.log(company);

Output

['LearnCodeOnline', 'iNeuron.ai', 'Learnyst', 'FindCoder']

Alternate way to create an array

Syntax

const array_name = new Array("itemA", "itemB", "itemC");

Example

const company = new Array("LearnCodeOnline", "iNeuron.ai", "Learnyst", "FindCoder");
console.log(company);

Output

['LearnCodeOnline', 'iNeuron.ai', 'Learnyst', 'FindCoder']

Access the Elements of an Array

Indexes of an array are used to retrieve the elements of an array.

Indexing of elements begins from 0 in arrays means first element index is zero

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst", "FindCoder"];
console.log(company[0]);
console.log(company[1]);
console.log(company[2]);

Output

LearnCodeOnline
iNeuron.ai
Learnyst

Array Methods

1. push()

This method adds a new element to the array at the end.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst", "FindCoder"];
company.push("Codercommunity.io");
console.log(company);

Output

['LearnCodeOnline', 'iNeuron.ai', 'Learnyst', 'FindCoder', 'Codercommunity.io']

2. pop()

This method deletes an element of the array which is present at the end.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst", "FindCoder"];
company.pop();   //this line deletes FindCoder
console.log(company);

Output

['LearnCodeOnline', 'iNeuron.ai', 'Learnyst']

3. shift()

This method removes the first element of the array and shifts all other elements to a lower index.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst", "FindCoder"];
company.shift();   //this line deletes LearnCodeOnline
console.log(company);

Output

['iNeuron.ai', 'Learnyst',  'FindCoder']

4. unshift()

This method adds one or more elements to the array starting at the beginning of the array.

Example

const company = ["iNeuron.ai", "Learnyst", "FindCoder"];
company.unshift("LearnCodeOnline");   //this line adds LearnCodeOnline
console.log(company);

Output

['LearnCodeOnline', 'iNeuron.ai', 'Learnyst',  'FindCoder']

5. concat()

This method creates a new array by concatenating existing arrays.

Example

const company1 = ["LearnCodeOnline", "iNeuron.ai"];
const company2 = ["Learnyst", "FindCoder"];
const company = company1.concat(company2);  //this line concatenates arrays
console.log(company);

Output

['LearnCodeOnline', 'iNeuron.ai', 'Learnyst',  'FindCoder']

6. slice()

This method slices out a piece of an array into a new array. This method does not remove any elements from the source array. This method has two parameters start and end. A start has a default value of 0, thus if the start is not specified, it will take that value by default.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst", "FindCoder"];
const newArray = company.slice(1,3);
console.log(newArray);

Output

['iNeuron.ai', 'Learnyst']

7. splice()

This method can be used to add new items to an array. The first parameter defines the position where new elements should be added. The second parameter defines how many elements should be removed.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst"];
company.splice(1,0, "FindCoder");
console.log(company);

Output

['LearnCodeOnline', 'FindCoder', 'iNeuron.ai', 'Learnyst'];

8. toString()

This method automatically converts an array to a comma-separated string when a primitive value is expected and then console logs it.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst"];
company.toString();

Output

LearnCodeOnline,iNeuron.ai,Learnyst

9. join()

This method also joins all array elements into a string. It behaves similarly to toString() but we can specify the separator.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst"];
let companyStr = company.join(" & ")
console.log(companyStr);

Output

LearnCodeOnline & iNeuron.ai & Learnyst

10. reverse()

This method reverses the order of the elements in the array.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst"];
company.reverse();

Output

['Learnyst', 'iNeuron.ai', 'LearnCodeOnline']

11. sort()

This method sorts the array's elements alphabetically. Sorting means placing the components in either ascending or descending order.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst"];
company.sort();

Output

['LearnCodeOnline', 'Learnyst', 'iNeuron.ai']

12. includes()

This method returns a boolean value if a string contains a specified string "true" if present and "false" if not present.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst"];
let result = company.includes("iNeuron.ai");
console.log(result)

Output

true

13. indexOf()

This method returns the position of the first occurrence of a value in the array. If the value cannot be found, -1 is returned. This method has two parameters the item we want to search and the position from where we want to start the search in the start.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst"];
let result1 = company.indexOf("iNeuron.ai");
let result2 = company.indexOf("iNeuron.ai" ,1);
console.log(result1);
console.log(result2);

Output

1
1

14. lastIndexOf()

This method returns the last index of the first occurrence of a value in the array. If the value cannot be found, -1 is returned. This method has two parameters the item we want to search and the position from where we want to start the search in the start.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst", "iNeuron.ai"];
let result1 = company.indexOf("iNeuron.ai");
let result2 = company.indexOf("LearnCodeOnline" ,1);
console.log(result1);
console.log(result2);

Output

3
-1

15. find()

This method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

Example

const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);

Output

12

16. findIndex()

This method returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

Example

const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));

Output

3

17. every()

This method tests whether all elements in the array pass the test function. It returns a Boolean value.

Example

const isBelowThreshold = (currentValue) => currentValue < 40;
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));

Output

true

18. map()

This method creates a new array populated with the results of calling a provided function on every element in the calling array.

Example

const array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x * 2);
console.log(map1);

Output

[2, 8, 18, 32]

19. filter()

This method generates a new array with the elements that satisfy the function's requirement. The initial array is unaltered.

Example

const company = ["LearnCodeOnline", "iNeuron.ai", "Learnyst", "FindCoder"];
const result = company.filter(word => word.length > 10);
console.log(result);

Output

['LearnCodeOnline']

20. reduce()

This method makes a call to or runs the function on each element of the array and outputs a single value (the function's aggregated result).

Example

const numbers = [175, 50, 25];
res = numbers.reduce(myFunc);
function myFunc(total, num) 
{
  return total - num;
}
console.log(res)

Output

100

Well! That's quite a long article🥲. If you enjoyed then do like and share, Thanks, see you in the upcoming article. ✌️

Did you find this article valuable?

Support The Night Coder's Blog by becoming a sponsor. Any amount is appreciated!