Array functions in JavaScript part-1

A JavaScript array can:
- Hold values of different types.
- The length of an array is dynamically sized and auto-growing.
Most of the array functions are used to transform or manipulate an array in some way.
In this post we will cover the following array functions:
- every()
- Some()
- sort()
- filter()
- join()
every():
Checks if every element of an array pass the test.
* * *
/ / checks every element in the array is greater than zero
let numbers = [1, 3, 5];
let result = numbers.every( e =>e > 0);
some():
Checks if at least one element in the array passes the test.
* * *
// checks at least one element in the array is less than five
let marks = [ 4, 5, 7, 9, 10, 3 1];
sort():
Sorts elements in place and returns the sorted array. Sorts the array in ascending order by default.
Casts elements to strings and compares them to determine the order.
* * *
const months = [“March’, ‘Jan’, ‘Feb’, ‘Dec’];
months.sort( );
console.log( months) ;
/ /output: Array [“Dec”, “Feb”, “Jan”, “March”]
let less Than Five = marks. some(e => e < 5);
filter():
Creates a new array with all elements that pas the test implemented by the provided function.
* * *
const words = [‘spray’, limit’, ‘elite’, ‘exuberant’,
destruction’ , ‘present ‘];
const result = words.filter (word =>Word.length> 6);
console. log( result) ;
/ / output: Array [“exuberant”, “destruction”, “present “]
join():
Concatenates all elements of an array into a string separated by a separator.
* * *
const elements = [‘Fire’, ‘Air’, ‘Water’];
console. log(elements.join( ));
// output: “Fire , Air, Water”
console. log(elements.join(‘-‘));
// output: “Fire-Air-Water”