JavaScript Some Arrays Collection Example

Hello devs in this tutorial i will discuss about JavaScript array. I will show you the example code of javascript array of objects, javascript array map, javascript array contains, javascript array foreach, javascript array methods, javascript array push, javascript array filter etc.

I will show you one afer another with source code and example so that you can unserstand JavaScript array. Let's start.

unshift()

This unshift() method adds the new element at the beginning of the array and returns the new length of the array.

Example

const array = ["world"];
array.unshift("hello"); // 2

console.log(array); // ["hello", "world"]

 

shift()

The shift() method removes the first element from the array and returns the removed element. It also changes the length of the array.

Example

const array = ["hello", "world"];
array.shift(); // "hello"

console.log(array); // ["world"]

 

join()

The join() method creates and returns a new string by concatenating all of the elements in an array separated by commas or a specified separator string.

Example

const array1 = ["1", "2", "3"];
array1.join(); // "1,2,3"

const array2 = ["I", "love", "programming"];
array2.join("-"); // "I-love-programming"

 

concat()

The concat() method is used to merge two or more arrays. This method does not change the existing arrays but instead returns a new array.

Example

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

console.log(array3); // ["a", "b", "c", "d", "e", "f"]

 

every()

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Example

const array = [10, 2, 1, 13, 17, 19, 6, 9];

array.every(item => item > 4) // false
array.every(item => item < 20) // true

 

map()

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

Example

const array = [1, 2, 3, 4, 5];

const doubleOfArray = array.map(item => item * 2);

console.log(doubleOfArray); // [2, 4, 6, 8, 10]

 

filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Example

const array = [1, 2, 3, 4, 5];

// only the element that are even
const evenArray = array.filter(item => item % 2 === 0);

console.log(evenArray); // [2, 4]

 

sort()

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending.

Example

const months = ['March', 'Jan', 'Feb', 'Dec'];
const nums = [4, 6, 2, 5, 1, 7, 3]

months.sort();
nums.sort();

console.log(months); // ["Dec", "Feb", "Jan", "March"]
console.log(nums); // [1, 2, 3, 4, 5, 6, 7]

 

reverse()

The reverse() method reverses an array in place and returns the sorted array. Don't confuse it with sorting in descending order.

Example

const nums = [4, 6, 2, 5, 1, 7, 3]

nums.reverse();

console.log(nums); // [3, 7, 1, 5, 2, 6, 4]

 

Read aslo : JavaScript Pass Function as Parameter with Example

 

Hope this JavaScript array example tutorial can help you to learn new things.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js