Arrays in JavaScript

An array can hold multiple values at a time.

To create an array in JavaScript:
Create an Array in JavaScript by assigning it to square brackets

let  myArray = [];

Assigning values to Array in JavaScript

let myArray = [1, 2, 3]; 
//Access the values of Array
console.log(myArray[0]); // The output is: 1
console.log(myArray[1]); // The output is: 2 
console.log(myArray[2]); // The output is : 3
console.log(myArray[3]); // The output is: undefined

Use length to get the number of elements in an Array

The length property returns the number of elements in an Array.

let myArray = [1, 2, 3];  
console.log( myArray.length ); // The output is 3

Push Method to add elements to Array

To add an element to an Array use the push method.

let myArray = [];
myArray.push(4);
console.log( myArray );
//The output is : [4]

Pop Method: Remove the last element from the Array

The pop method removes the last element from the Array and returns that element.

let myArray = [1,2,3];
let lastElement = myArray.pop();
console.log( lastElement );
//The output is : 3

Shift Method: Removes and returns the first element from the Array

let myArray = [1,2,3];
let firstElement = myArray.shift();
console.log( firstElement );
//The output is : 1 

Splice Method: Removes elements from the Array from the index number specified

let myArray = [1,2,3,4];
myArray.splice(3,1);
console.log( myArray );
//The output is : [1,2,3]

Note: 1 here indicates how may numbers to be deleted after the index number specified.

Splice method can also add elements to an Array.

let myArray = [1,2,3,4];
myArray.splice(3,1,5,6,7); 
console.log( myArray );
//The output is: [1,2,3,5,6,7] 

Here element at index number 3 is deleted. The number of elements to be deleted is 1. So in the above myArray 4 is deleted. The elements 5,6,7 is then added to the array.

Below is the list of all the methods and properties of an Array.

JavaScript Array Properties

constructor: Function to create Array’s objects
length: Gives you the number of elements in an array
prototype:You can add properties and methods to an Array object using prototype

JavaScript Array Methods

concat():Joins two or more arrays, and return the joined array
copyWithin():To copy array elements within the array, to and from specified positions
entries():Returns Array Iteration Object
every():Checks applied to every element in an array to pass a test
fill():Fill array with a static value
filter():Creates a new array with some check applied
find():Returns first element in an array to comply with check
findIndex():Returns index of first element to comply with check
forEach():Call a function for each array element
from():Get array from an object
includes():Checks if an array contains the specified element
indexOf():Returns index position of the specified element
isArray():To check if object is an Array or not
join():Joins all elements of an array and create a string
keys():Returns an Array with the keys of the original array
lastIndexOf():Return index of the specified element startin from the last
map():A new array is created with the result of calling a function for each array element
pop():Removes the last element from array, and returns that element
push():Adds an element to the end of an array, and returns the new length
reduce():Reduces to a single value (going left-to-right)
reduceRight():Reduce to a single value (going right-to-left)
reverse():Reverse the order of elements
shift():Removes first element of array, and return that element
slice():Removes or adds a part of an array, and returns the new array
some():Checks if any of the elements in an array complies with a test
sort():Sort array elements
splice():Adds/Removes array elements
toString():Converts array elements to string
unshift():Add elements to the beginning of array, and return the new length
valueOf():Gets the primitive value of an array

Leave a Reply

Your email address will not be published. Required fields are marked *