Skip to main content

Command Palette

Search for a command to run...

Array and Functions

Updated
6 min readView as Markdown
Array and Functions

Array

What is Array in JavaScript?

In computer science, an array data structure, or simply an array, is a data structure
consisting of a collection of elements
                                                                                                                -Your Friend Google

but I am not a coder and I do not know what is data structures and all, how can I understand what are arrays? Do not worry I help you to understand each bit of the array. So let's get started, you can understand the array with an easy example of 10 houses on street A. House Numbers starts from 0 to 9.

houses.PNG

Note Index Starts from 0 in Coding

Now you can say that street A is an array which has 10 houses in respect of computers.

Collection of elements in coding is called Array.                                                                                       -Sarthak🙂

In our example, there was a collection of houses on Street A which is why Street A is considered an array.

How to create an array?

An array is created with the help of square brackets ' [] ' and elements inside an array are stored separated by a coma ' , '.

Syntax:- let arrayName=[]

let arrayName=[] \\empty array
let arrayName1=[1,2,3,4,5] \\ array of Numbers
let arrayName2=['Raj','Rohan','Ram','Shyam'] \\ array of strings

we can also create an array using constructors

Syntax:- let arrayName= new Array()

let name=new Array('Raj','Rohan','Ram','Shyam')

How to access the elements stored in an array?

To understand this let's go to Street A, suppose you have to send a letter to a friend who lives on Street A for that you should know his/her house number so that you can send your letter to him/her, similarly in the array we need to pass the index of the element to access the element. Here you can say that the index is the address of that element.

Syntax:- nameofthearray[index]

Code
let arr=[1,2,3,4,5,6,7,8,9]
console.log(arr[0])
console.log(arr[1])
console.log(arr[2])
Output

output1.PNG

Functions In Array

To manipulate elements in an array we use different functions

  • toString
  • join
  • pop

are few functions. Now let's discuss functions one by one

toString Function

toString help to convert our array to a normal string.

Syntax: arrayName.toString()

Code
let arr=[1,2,3,4,5,6]
console.log(arr) 
Output

output2.PNG

The output here is enclosed in ' [] ' which is used to declare an array so to convert the array to a string we use the toSting function

Code
let arr=[1,2,3,4,5,6]
console.log(arr.toString()) 
Output

output3.PNG

Join Function

The join function is the same as toString, it converts the array to string but gives us an additional function i.e we can select by which we want to separate our elements.

arrayName.join(" ")

Code
let arr=[1,2,3,4,5,6]
console.log(arr.join(" * "))
console.log(arr.join("@"))
output

output4.PNG

Now let's discuss how to add and delete any element from the end

Pop Function

The pop function is used to delete or remove an element from the last of the array

arrayName.pop()

Code
let arr=[1,2,3,4,5,6]
arr.join()
console.log(arr)
output

output5.PNG

Push Function

The push function is used to add an element at the last position of the array

arrayName.push()

Code
let arr=[1,2,3,4,5,6]
arr.push(7)
console.log(arr)
output

output6.PNG

Now let's discuss how to add and delete any element from the start

Shift Function

The Shift function is used to delete or remove an element from the start of the array

arrayName.shift()

Code
let arr=[1,2,3,4,5,6]
arr.shift()
console.log(arr)
output

output7.PNG

UnShfit Function

The Unshift function is used to add an element at the start position of the array

arrayName.unshift()

Code
let arr=[1,2,3,4,5,6]
arr.unshift(0)
console.log(arr)
output

output8.PNG

Adding Element at the particular position

To add an element at any given position we need to use index. Now if the index is empty then the value is assigned to the location and if there is anything present, then the current value present is replaced by the new value.

Syntax arrayName[index]=newValue

Code
let arr=[1,2,3,4,5,6]
arr[6]=7
arr[0]="Ram"
console.log(arr)
Output

output9.PNG

Length Of An Array

To find the length of the array we use the length function

Syntax:- arrayName.length

Code
let arr=[1,2,3,4,5,6]
console.log(arr.length)
Output

output10.PNG

Delete element from a given index

To delete any element from a given index we use the delete function

Syntax:- delete arrayName[index]

Code
let arr=[1,2,3,4,5,6]
delete arr[5]
console.log(arr)
Output

11.PNG

Concatenating a.ka.a Merging Arrays

Case 1

If we want to merge two given arrays we use concatenation

Syntax:- arrayName.concat(arrayMerge)

Code
let arr=[1,2,3]
let arr1=[4,5]
let arr2=[6,7]
console.log(arr.concat(arr1,arr2))
Output

12.PNG

Case 2

Similarly, we can merge single values also

Syntax:- arrayName.concat(value)

Code
let arr=[1,2,3]
console.log(arr.concat(4))
Output

13.PNG

Splicing and Slicing Arrays

###Splice Function The splice() method can be used to add new items to an array.

Syntax: - arrayName.splice(start-index, number-of-elements-need-to-modify,value)

Code
const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits)
fruits.splice(2, 1, "Lemon", "Kiwi");
console.log(fruits)
Output

14.PNG

If we define 0 at the place of 1 then Lemon and Kiwi both will be added at position 2. Splice does not affect the values of the array if we mention 0 instead 1.

Slice Function

The Slice Function divide the array into the given range.

Syntax arrayName.slice(start,end)

Code
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
console.log('Array with Slice')
console.log(fruits)
const citrus = fruits.slice(1, 3)
console.log('Slice of the array is taken out from index 1 to 3 and stored in   Citrus')
console.log(citrus)
Output

15.PNG

Sorting an Array

The sort function helps us to sort elements of an array.

Syntax:- arrayName.sort()

Code
const fruits = ["Banana", "Orange", "Apple", "Mango"]
console.log(fruits)
fruits.sort()
console.log(fruits)
Output

16.PNG

Iteration of array

Iteration of the array means getting hold of each element of the array or travelling to every element of the array

Using For loop

We can get hold of elements using for loop.

Code
const fruits = ["Banana", "Orange", "Apple", "Mango"]
console.log('Following are the elements of the array')
for(let i=0; i<fruits.length;i++){
    console.log(fruits[i])
}
Output

17.PNG

Using For Of Loop

This is used when we need to travel the whole length of the array.

Code
const fruits = ["Banana", "Orange", "Apple", "Mango"]
console.log('Following are the elements of the array')
for(let fruit of fruits){
    console.log(fruit)
}
Output

17.PNG