Skip to main content

Command Palette

Search for a command to run...

What is Array & there Methods

Published
5 min readView as Markdown
What is Array & there Methods
A

Hey There, I am going to start my new blogging journey, as I am learning full stack web development and I will sharing my all learning here. Keep learning Keep Growing :)

What is Array?

The array can store different types of data in a single variable whether it will be a string or number.

Syntax for Array

let Variable_name=[item1,item2,.....]

Example

Suppose you want to create a variable with different types of car models so we can achieve this using array.

let Cars=["Honda","Maruti Suzuki","Audi","BMW"]

Array Methods

Array Length

The Length method of the Array can be used to find out the total number of items present in the array.

let Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
console.log(Car.length);

//OUTPUT :-4

Adding element in Array

By using the PUSH method we can add the element to the array. Using this method new element can be added at the end of the array. it will work the same as a stack.

Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
Car.push("Range Rover"); 
console.log(Car); 

//Output:- [ 'BMW', 'AUDI', 'Scorpio', 'Maruti Suzuki', 'Range Rover' ]

Removing elements from the Array

By using the POP method we can remove the element from the array. Using this method we can remove the last element from the array.

Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
Car.pop(); 
console.log(Car);

Slice

As the word define slice means it can divide the array in two-part base on the parameters.

Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
let test =Car.slice(1,3); 
console.log(test);

//OUTPUT:-[ 'AUDI', 'Scorpio' ]

In the above example, we pass two parameters to the slice method

  1. start argument to define the from where the array will divide

  2. end argument defines the till where need to pick the value from the array except that mentioned index value.

Splice

The splice method can be used for adding elements to the array

let Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
Car.splice(1,2,"Range Rover","TATA NEO"); 
console.log(Car);



//OUTPUT:-[ 'BMW', 'Range Rover', 'TATA NEO', 'Maruti Suzuki' ]

The first parameter (1) defines the position where new elements should be added (spliced in).

The second parameter (2) defines how many elements should be removed.

The rest of the parameters ("Range Rover", "TATA NEO") define the new elements to be added.

Join

This method can be used to concatenate two arrays and make a single array.

let Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
let Fruit=["Mango","Apple","bannana"]
let join= Car.concat(Fruit);
console.log(join);

//OUTPUT:-['BMW', 'AUDI', 'Scorpio', 'Maruti Suzuki', 'Mango', 'Apple', 'bannana']

Reverse

The Reverse function can be used to reverse the string.

let Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
Car.reverse()
console.log(Car);

//OUTPUT:-[ 'Maruti Suzuki', 'Scorpio', 'AUDI', 'BMW' ]

As we check in the above example the order of the Car array has been reversed the last element comes first and the first element goes last.

Shift

The Shift method can be used to remove the first element of the array

let Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
Car.shift()
console.log(Car);

//OUTPUT:-["AUDI","Scorpio","Maruti Suzuki"]

As per the above example, we checked by using the shift method first element of the array has been removed from the list.

Unshift

The Unshift method can be used to add elements at the beginning of the array.

let Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
Car.unshift("Range Rover")
console.log(Car)
//OUTPUT:-["Range Rover","BMW","AUDI","Scorpio","Maruti Suzuki"]

As per the above example, we checked by using the unshift method we add "Range Rover" at the beginning of the array.

Sort

The Sort method can be used to sort arrays in ascending order.

let Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
 let test=Car.sort()
console.log(test)

//OUTPUT:-[ 'AUDI', 'BMW', 'Maruti Suzuki', 'Scorpio' ]

As per the above example, we checked by using the sort method we sort the array in ascending order.

Split

The Split can be used to split the array in the number of substrings.

let Car=("Javascript")
let test=Car.split("");
console.log(test);

//OUTPUT:-[ 'J', 'a', 'v', 'a','s', 'c', 'r', 'i','p', 't']

As per the above example, we checked by using the split method we split the string "Javascript" into different substrings and make an array.

Indexof

The Indexof method can be used to find out the index of character in the array

let Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
let test=Car.indexOf("AUDI");  
console.log(test);

//OUTPUT:-1

As per the above example, we checked by using the indexof method we found that the index of the best word is 19.

Includes

The Includes method can return the boolean value and it checks whether the particular string was present or not if the string was present then it will be written as true if not present then it will return false.

let Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
let test=Car.includes("AUDI");  
console.log(test);

//OUTPUT:-true

let Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
let test=Car.includes("tata");  
console.log(test);

//OUTPUT:-false

As per the above example, the best word was present in the string so it returns true and in the next example, the python word was not present so it returns false.

Substr

The Substr method can be used to extract particulars word from the string base on the index number.

let Car=("Javascript is very best language")
let test=Car.substring(0,10)
console.log(test);

//OUTPUT:-Javascript

In the above example,

(0) define the first index of the array from that we need to extract data.

(10) define the till which index needs to extract the data.

as we check in the above example (0) index of the array is 'J' so and 't' is the 10th index of array so the output is 'Javascript'.

Max

The Max method can be used to define Max element of the array.

console.log(Math.max(1,34,52,54,32))

//OUTPUT: 54

Min

The Min method can be used to define the Max element of the array.

console.log(Math.max(1,34,52,54,32))

//OUTPUT: 1

Map

The Map method creates a new array from the results of calling a function for every element.

let maths = [4, 9, 49, 16, 25];
 console.log(maths.map(Math.sqrt));

//OUTPUT: [2,3,7,4,5]

As we check in the above example my using the 'math.sqrt' method we find out the square root of all the numbers present in the array and using the map method we can create a new array where all square has a store.

ToString

The ToString method can be used to convert an array to a string.

let Car=["BMW","AUDI","Scorpio","Maruti Suzuki"]
let test= Car.toString();
console.log(test)

//OUTPUT:-BMW,AUDI,Scorpio,Maruti Suzuki

As we check in the above example by using the 'toString' method we converted the array to a string.