JavaScript Arrays

JavaScript Arrays

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.

Untitled.png

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

2.PNG

Array.isArray()

Returns true if the argument is an array, or false otherwise.

Code

const arr=[1,true];
console.log(Array.isArray(arr));

Output

1.PNG

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

1.PNG

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

1.PNG

Instance Methods

Arrary.prototype.at()

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

1.PNG

Negative Integer:

Code

const array1 = [5, 12, 8, 130, 44];
console.log(array1.at(-1));

Output

1.PNG

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

1.PNG

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

2.PNG

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

1.PNG

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

1.PNG

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

1.PNG

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

1.PNG

Code

const array1 = ['1', '2', '3', '4', '5'];
console.log(array1.includes("7"));

Output

2.PNG

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

1.PNG

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

3.PNG

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

1.PNG

If Element is not found

Code

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
//indexOf(searchElement)
console.log(animals.indexOf("cat"));

Output

1.PNG

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

2.PNG

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

1.PNG

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

image.png

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

1.PNG

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

3.PNG


For more methods, Check out the MDN docs.

Happy Learning!