What Are Arrays in JavaScript
What Are Arrays in JavaScript
NB: I used Arrow functions in this post, If you don’t know what this means,
you should read here. Arrow function is an ES6 feature.
toString()
The JavaScript method toString() converts an array to a string
separated by a comma.
let colors = ['green', 'yellow', 'blue'];
console.log(colors.toString()); // green,yellow,blue
join()
The JavaScript join() method combines all array elements into a string.
It is similar to toString() method, but here you can specify the
separator instead of the default comma.
let colors = ['green', 'yellow', 'blue'];
console.log(colors.join('-')); // green-yellow-blue
concat
This method combines two arrays together or add more items to an
array and then return a new array.
push()
This method adds items to the end of an array and changes the original
array.
let browsers = ['chrome', 'firefox', 'edge'];
browsers.push('safari', 'opera mini');
console.log(browsers);
// ["chrome", "firefox", "edge", "safari", "opera mini"]
pop()
This method removes the last item of an array and returns it.
let browsers = ['chrome', 'firefox', 'edge'];
browsers.pop(); // "edge"
console.log(browsers); // ["chrome", "firefox"]
shift()
This method removes the first item of an array and returns it.
let browsers = ['chrome', 'firefox', 'edge'];
browsers.shift(); // "chrome"
console.log(browsers); // ["firefox", "edge"]
unshift()
This method adds an item(s) to the beginning of an array
and changes the original array.
let browsers = ['chrome', 'firefox', 'edge'];
browsers.unshift('safari');
console.log(browsers); // ["safari", "chrome", "firefox", "edge"]
You can also add multiple items at once
splice()
This method changes an array, by adding, removing and inserting
elements.
The syntax is:
slice()
This method is similar to splice() but very different. It returns subarrays
instead of substrings.
This method copies a given part of an array and returns that copied
part as a new array. It does not change the original array.
The syntax is:
array.slice(start, end)
split()
This method is used for strings. It divides a string into substrings and
returns them as an array.
Here’s the syntax:string.split(separator, limit);
another example:
indexOf()
This method looks for an item in an array and returns the index where
it was found else it returns -1
let fruits = ['apple', 'orange', false, 3]
fruits.indexOf('orange'); // returns 1
fruits.indexOf(3); // returns 3
friuts.indexOf(null); // returns -1 (not found)
lastIndexOf()
This method works the same way indexOf() does except that it works
from right to left. It returns the last index where the item was found
let fruits = ['apple', 'orange', false, 3, 'apple']
fruits.lastIndexOf('apple'); // returns 4
filter()
This method creates a new array if the items of an array pass a certain
condition.
map()
This method creates a new array by manipulating the values in an array.
Example:
reduce()
This method is good for calculating totals.
To loop through an array and sum all numbers in the array up, we can use
the for of loop.
const numbers = [100, 300, 500, 70];
let sum = 0;
for (let n of numbers) {
sum += n;
}
console.log(sum);
The snippet below shows how the reduce() method works with all four
arguments.
More insights into the reduce() method and various ways of using it
can be found here and here.
forEach()
This method is good for iterating through an array.
every()
This method checks if all items in an array pass the specified condition
and returntrue if passed, else false.
check if all numbers are positive
const numbers = [1, -1, 2, 3];
let allPositive = numbers.every((value) => {
return value >= 0;
})
console.log(allPositive); // would return false
some()
This method checks if an item (one or more) in an array pass the
specified condition and return true if passed, else false.
includes()
This method checks if an array contains a certain item. It is similar
to .some(), but instead of looking for a specific condition to pass, it
checks if the array contains a specific item.
let users = ['paddy', 'zaddy', 'faddy', 'baddy'];
users.includes('baddy'); // returns true