Friday, December 27, 2024
Google search engine
HomeLanguagesJavascriptBest-Known JavaScript Array Methods

Best-Known JavaScript Array Methods

An array is a special variable in all the languages that are used to store different elements. JavaScript array contains some built-in properties that every JavaScript developer should know how to use and when or where to use them. We can use them to add, remove, iterate, or manipulate data as per our requirements.

Basic JavaScript Array Methods:

There are some Basic JavaScript array methods that every developer should know are:

1. Array some() Method:

This method checks whether at least one of the elements of the array satisfies the condition checked by the argument function.

Example:

Here is an example of the basic use of the Array some() method.

Javascript




// JavaScript to illustrate
// lastIndexOf() method
function isGreaterThan5(element, index, array) {
    return element > 5;
}
 
function func() {
    // Original array
    let array = [2, 5, 8, 1, 4];
 
    // Checking for condition in array
    let value = array.some(isGreaterThan5);
 
    console.log(value);
}
 
func();


Output

true

2. Array reduce() Method:

The array reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.

Example: 

Here is an example of the basic use of the Array reduce() method.

javascript




// Original array
let numbers = [88, 50, 25, 10];
 
// Performing reduce method
let sub = numbers.reduce(neveropen);
 
function neveropen(total, num) {
    return total - num;
}
 
console.log(sub);


Output

3

3. Array map() Method:

The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method. Generally, the map() method is used to iterate over an array and call the function on every element of an array.

Example: 

Here is an example of the basic use of the Array map() method.

javascript




// Original array
let numbers = [4, 9, 16, 25];
 
// Performing map method
let sub = numbers.map(neveropen);
 
function neveropen() {
    return numbers.map(Math.sqrt);
}
 
console.log(sub);


Output

[ [ 2, 3, 4, 5 ], [ 2, 3, 4, 5 ], [ 2, 3, 4, 5 ], [ 2, 3, 4, 5 ] ]

4. Array every() Method:

This method checks whether all the elements of the array satisfy the given condition or not that is provided by a function passed to it as the argument.

Example: 

Here is an example of the basic use of the Array every() method.

javascript




// JavaScript code for every() function
function ispositive(element, index, array) {
    return element > 0;
}
 
function func() {
    let arr = [11, 89, 23, 7, 98];
 
    // Check for positive number
    let value = arr.every(ispositive);
 
    console.log(value);
}
 
func();


Output

true

5. Array flat() Method:

This method creates a new array that contains more than arrays. Basically creates a simple array from an array that contains multiple arrays.

Example: 

Here is an example of the basic use of the Array flat() method.

javascript




//Original array
let arr = [[11, 89], [23, 7], 98];
 
// Performing flat method
let neveropen = arr.flat();
 
console.log(neveropen);


Output

[ 11, 89, 23, 7, 98 ]

6. Array flatMap() Method:

This method is used to flatten the input array element into a new array. This method first of all map every element with the help of a mapping function, then flattens the input array element into a new array.

Example: 

Here is an example of the basic use of the Array flatMap() method.

javascript




const myAwesomeArray = [
    [1], [2], [3], [4], [5]
];
 
let neveropen = myAwesomeArray.flatMap(
    (arr) => arr * 10
);
console.log(neveropen);


Output

[ 10, 20, 30, 40, 50 ]

7. Array filter() Method:

This method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument function.

Example: 

Here is an example of the basic use of the Array filter() Method.

javascript




function isPositive(value) {
    return value > 0;
}
 
function func() {
    let filtered = [112, 52, 0, -1, 944]
    .filter(isPositive);
    console.log(filtered);
}
 
func();


Output

[ 112, 52, 944 ]

8. Array findindex() Method:

This method returns the index of the first element in a given array that satisfies the provided testing function. Otherwise, -1 is returned.

Example: 

Here is an example of the basic use of the Array.findindex() method.

javascript




let array = [10, 20, 30, 110, 60];
 
function finding_index(element) {
    return element > 25;
}
 
console.log(array
    .findIndex(finding_index)
);


Output

2

9. Array find() Method:

This method is used to get the value of the first element in the array that satisfies the provided condition. It checks all the elements of the array and whichever the first element satisfies the condition is going to print.

Example:

Here is an example of the basic use of the Array find() method.

javascript




// Input array contain some elements.
let array = [10, 20, 30, 40, 50];
 
// Function (return element > 10).
let found = array.find(function (element) {
    return element > 20;
});
 
// Printing desired values.
console.log(found);


Output

30

10. Array fill() Method:

This method is used to fill the array with a given static value. The value can be used to fill the entire array or it can be used to fill a part of the array.

Example: 

Here is an example of the basic use of the Array fill() Method.

javascript




// JavaScript code for fill() function
let arr = [1, 23, 46, 58];
 
// Here value = 87, start index = 1 and
// and last index = 3
arr.fill(87, 1, 3);
console.log(arr);


Output

[ 1, 87, 87, 58 ]

11. Array forEach() Method:

This method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array.

Example: 

Here is an example of the basic use of the Array forEach() Method.

javascript




// Original array
const items = [1, 29, 47];
const copy = [];
 
items.forEach(function (item) {
    copy.push(item * item);
});
 
console.log(copy);


Output

[ 1, 841, 2209 ]

12. Array sort() Method:

This method is used to sort the array. An array can be of any type i.e. string, numbers, characters, etc.

Example: 

Here is an example of the basic use of the Array sort() Method:

javascript




// Original array
let numbers = [88, 50, 25, 10];
 
// Performing sort method
let sub = numbers.sort(neveropen);
 
function neveropen(a, b) {
    return a - b;
}
 
console.log(sub);


Output

[ 10, 25, 50, 88 ]

13. Array concat() Method:

This method is used to merge two or more arrays together. This function does not alter the original arrays passed as arguments.

Example: 

Here is an example of the basic use of the Array concat() Method.

javascript




// JavaScript code for concat() function
let num1 = [11, 12, 13],
    num2 = [14, 15, 16],
    num3 = [17, 18, 19];
 
console.log(num1.concat(num2, num3));


Output

[
  11, 12, 13, 14, 15,
  16, 17, 18, 19
]

14. Array includes() Method:

This method is used to know whether a particular element is present in the array or not and accordingly, it returns true or false i.e, if the element is present, then it returns true otherwise false

Example: 

Here is an example of the basic use of the Array includes() Method.

javascript




// Taking input as an array A
// having some elements.
let A = [1, 2, 3, 4, 5];
 
// Include() function is called to
// test whether the searching element
// is present in given array or not.
let a = A.includes(2);
 
// Printing result of function.
console.log(a);


Output

true

15. Array reverse() Method:

This method is used for in-place reversal of the array. The first element of the array becomes the last element and vice versa.

Example: 

Here is an example of the basic use of the Array reverse() method

javascript




//Original Array
let arr = [34, 234, 567, 4];
console.log("Original array: " + arr);
 
//Reversed array
let new_arr = arr.reverse();
 
// Display the result
console.log("Newly reversed array: " + new_arr);


Output

Original array: 34,234,567,4
Newly reversed array: 4,567,234,34
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments