Skip to main content

Command Palette

Search for a command to run...

Array in JavaScript

Published
•4 min read•View as Markdown
Array in JavaScript

Array is most common data type across all Computer Languages. It can stores more than one value in it that all can be of multiple types. it is like a container for storing information and can be very useful in real world. We commonly use array and hence it is powerful too in JavaScript with built functions JavaScript provides us. We thoroughly look which all are commonly used functions in array.

In JavaScript declaration of array is of many types. This is the one common way :

const myArray = [ "value1" , "value2" , "and so on" ];

MERGING TWO OR MORE ARRAY

Suppose one day we feel need to merge information of all late comings in office and leaves taken in FY that we have stored in two different arrays. In this case we can merge these details and derive details at one place. Yeah i gave some weird example but you understood. it can be effective when we have large chunks of values stored.
const myArray1 = [  " a " ," b ", " c "];
const myArray2 = [  " d " ," e ", " f "];
const myArray3 = myArray1.concat(myArray2);

COPYING ARRAY'S ELEMENTS WITHIN ARRAY ITSELF

Array.copyWithin( target ( where do you want to copy ) , start ( starting ( index ) range of elements to be copied) , end ( end range ( index ) it is always excluded i.e end - 1 ) ); Whenever we want to copy some data on target index with in the array. We use this function of javaScript. it removes the original values from array and modify the array with updated values.

const array1 = ['a', 'b', 'c', 'd', 'e'];
console.log(array1.copyWithin(2, 3, 4));
output : [ 'a', 'b', 'd', 'd', 'e' ]

EVERY --- WE PUT CONDITION ON OUR STORED INFORMATION TO DETERMINE OUR DATA BASED ON VALUES

It returns a boolean value i.e true or false.

const isEven = (element) => element %  2 == 0;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isEven));

output would be false

FILL ARRAY Array.fill( ); it usually fills the value on till indexes that we pass in it. array.fill( value , start (index) , end (index) );

const array1 = [1, 2, 3, 4];
console.log(array1.fill(0, 2, 4));
output would be [ 1, 2, 0, 0 ]

FILTER ARRAY

it filter down the array elements whatsoever condition we apply into function

const words = ['push', 'your', 'limit', 'never', 'give ', 'up'];

const result = words.filter(word => word.length > 1);

console.log(result);
output would be  [ 'push', 'your', 'limit', 'never', 'give ', 'up' ]; 
it will return array as it as all the elements has length larger than one.

ARRAY TO STRING It let the array converted into string without commas

const fruits = ["Banana", "Orange", "Apple", "Mango"];
const fruitsUpdated = fruits.toString();
console.log(fruitsUpdated);
output : Banana,Orange,Apple,Mango

ARRAY POP( ) it removes the value of last index and update the array.

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();
console.log(fruits);
output : [ 'Banana', 'Orange', 'Apple' ]

ARRAY PUSH The push() method adds a new element to an array (at the end):

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
console.log(fruits);
output :  [ 'Banana', 'Orange', 'Apple', 'Mango', 'Kiwi' ]

ARRAY SHIFT ( ) Shifting is equivalent to popping, but working on the first element instead of the last.

const fruits = ["A", "B", "C", "D"];
fruits.shift();
console.log(fruits);
output : [ 'B', 'C', 'D' ]

ARRAY UNSHIFT( ) it adds a new element to an array (at the beginning)

const fruits = ["A", "B", "C", "D"];
fruits.unshift("value1");
console.log(fruits);
output : [ 'value1', 'A', 'B', 'C', 'D' ]

ARRAY FIND array.find( ); The find() 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. const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found); output: 12

const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);
 output: 12

ARRAY SLICE Slice method returns a range of Elements whatsoever range we provide i.e start and end .

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));

// expected output: Array ["camel", "duck", "elephant"]

ARRAY SPLICE It changes the array on the indexes with the values that we pass.

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]