What is an Array?
Array is a special kind of variable which can hold different datatype of values in it.
Arrays Index starts with 0 .
JavaScript Arrays are resizable.
To fetch the values we use array name followed by brackets with index number in it i.e arr[0]
How to declare an Array?
Using Array literal is the simplest way to create array in JavaScript. Surely there are other ways too.
Syntax
const array_name = [item1,item2.....];
Code
const name=["Raj","Avan","Paul",3,true];
Few Important Methods
Static Methods
Array.from()
Creates a new Array instance from an array-like object or iterable object.
Code
const name="Vivek";
console.log(Array.from(name));
Output
Array.isArray()
Returns true if the argument is an array, or false otherwise.
Code
const arr=[1,true];
console.log(Array.isArray(arr));
Output
Array.of()
The Array.of() method creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments.
Code
console.log(Array.of("Vivek",2,true));
Output
Instance Properties
Array.prototype.length
The length property of an object which is an instance of type Array sets or returns the number of elements in that array.
Code
const arr = [1,2,3,true,"lco"];
console.log(arr.length);
Output
Instance Methods
The at() method takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.
Positive Integer:
Code
const array1 = [5, 12, 8, 130, 44];
console.log(array1.at(0));
Output
Negative Integer:
Code
const array1 = [5, 12, 8, 130, 44];
console.log(array1.at(-1));
Output
Array.prototype.pop()
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
Code
const array1 = [5, 12, 8, 130, 44];
console.log(array1.pop());
console.log(array1);
Output
Array.prototype.Push()
The push() method adds one or more elements to the end of an array and returns the new length of the array.
Code
const array1 = [5, 12, 8, 130, 44];
console.log(array1.push(90));
console.log(array1);
Output
Array.prototype.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.
Code
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);
Output
Array.prototype.copyWithin()
The copyWithin() method shallow copies part of an array to another location in the same array and returns it without modifying its length.
Code
const array1 = ['a', 'b', 'c', 'd', 'e'];
//copyWithin(target, start, end)
array1.copyWithin(0,3,5);
console.log(array1);
Output
Array.prototype.every()
The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
Code
const array1 = ['1', '2', '3', '4', '5'];
let output = array1.every(isPositive);
console.log(output);
function isPositive(elements){
return elements>0
}
Output
Array.prototype.includes()
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
Code
const array1 = ['1', '2', '3', '4', '5'];
console.log(array1.includes("3"));
Output
Code
const array1 = ['1', '2', '3', '4', '5'];
console.log(array1.includes("7"));
Output
Array.prototype.slice()
The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
Code
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
//slice(start, end)
console.log(animals.slice(1,3));
console.log(animals); // original array is not modified
Output
Array.prototype.splice()
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, we use slice().
Code
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
//splice(start, deleteCount, item1)
animals.splice(1,3,"Dog","Cow","Lion");
console.log(animals); // original array is modified
Output
Array.prototype.indexOf()
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
Code
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
//indexOf(searchElement)
console.log(animals.indexOf("elephant"));
Output
If Element is not found
Code
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
//indexOf(searchElement)
console.log(animals.indexOf("cat"));
Output
Array.prototype.lastIndexOf()
The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at from Index.
Code
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant', 'bison'];
//lastIndexOf(searchElement)
console.log(animals.lastIndexOf("bison"));
Output
Array.prototype.sort()
The sort() method sorts an array alphabetically.
Code
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant', 'bison'];
//sort()
console.log(animals.sort());
Output
Array.prototype.reverse()
The reverse() method reverses the elements in an array.
Code
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant', 'bison'];
//reverse()
console.log(animals.reverse());
Output
Array.prototype.shift()
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
Code
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant', 'bison'];
//shift()
console.log(animals.shift());
console.log(animals); // modifies the array
Output
Array.prototype.unshift()
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
Code
const animals = ['ant', 'bison', 'camel'];
//unshift()
console.log(animals.unshift("Lion"));
console.log(animals); // modifies the array
Output
For more methods, Check out the MDN docs.
Happy Learning!