When we have to hold multiple values in a single Variable, then we need array.
Let’s take an example of Students in a school. Student is a single variable but will hold multiple values. like, class, age, gender, marks etc.. So, If we want to hold all these values in a student, then we can use Array for that.
let arr = [12, 24, 56, 43]
Console.log(arr)
Console.log(arr[0])
Console.log(arr[1])
Console.log(arr[2])
Console.log(arr[3])
Console.log(arr[4])
Array returns undefined for the values not present in an Array.
We can store many types of value in a single array.
let arr1 = [12,24, 56, 43, true, "QAPeddia"]
Console.log(arr1)
Console.log(arr1[0])
Console.log(arr1[1])
Console.log(arr1[2])
Console.log(arr1[3])
Console.log(arr1[4])
Console.log(arr1[5])
Console.log(arr1[6])
Now, Let’s see the operators and methods related to Arrays.
Console.log(arr1.length)
arr1[6] = 89
Console.log(arr1)
arr1[0] = 96
Console.log(arr1)
Console.log(typeof, arr1)
let num = [1, 2, 3, 34, 4]
let b = num.toString() // b is a string now
Console.log(b)
Console.log(b, typeof b)
Console.log(num)
This function joins all the Elements of array and returns a string.
This function doesn’t modify the original array.
let c = num.join("_") // c is also a string now
Console.log(c, typeof c)
Console.log(num)
Console.log(num)
let r = num.pop
Console.log(num)
Console.log(r)
Console.log(num)
let s = num.push(56)
Console.log(num)
Console.log(s)
Console.log(num)
let sh = num.shift
Console.log(num)
Console.log(sh)
Console.log(num)
let us = num.unshift(78)
Console.log(num)
Console.log(us)
delete num[0]
Console.log(num)
Console.log(num.length)
let num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let num1 = [11, 12, 13, 14, 15, 16, 17, 18, 19]
let newArray = num.concat(num1)
Console.log(newArray)
Sort method modifies the sorting order of the origial array.
let num = [551, 22, 3, 14, 5, 6, 7, 8, 229]
num.sort()
Console.log(num)
The problem with this method is that, this method sorts alphabetically, which means, number starting with 1 will be sorted first, then number starting with 2 will be sorted, then number starting with 3 will be sorted and so on.
Now, if I want to sort the array in ascending order, then I need to use a function compare, Let’s see it
function compare(a, b) {
return a - b;
}
let num = [551, 22, 3, 14, 5, 6, 7, 8, 229]
num.sort(compare)
Console.log(num)
Now, if I want to sort the array in descending order, then I need to use compare function in a different way, Let’s see it
function compare(a, b) {
return b - a;
}
let num = [551, 22, 3, 14, 5, 6, 7, 8, 229]
num.sort(compare)
Console.log(num)
let num = [551, 22, 3, 14, 5, 6, 7, 8, 229]
num.reverse()
Console.log(num)
let num = [551, 22, 3, 14, 5, 6, 7, 8, 229]
let deletedValues = num.splice[2, 3, 1021, 1022, 1023, 1024, 1025] //This function will perform function from an index of 2, will remove 3 numbers from index of 2 and then add the numbers from index of 2
Console.log(num)
Console.log(deletedValues)
let num = [551, 22, 3, 14, 5, 6, 7, 8, 229]
let newNum = num.slice(3)
Console.log(num)
Console.log(newNum)
let newNum1 = num.slice(3,5)
Console.log(newNum1)
We have covered Arrays and Arrays methods in this session.
Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…
We will start from object oriented programming basics like What are different concepts in Object…
Why we can not over ride a Constructor This is a very important question, that,…
What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…
What is Anonymous Function Anonymous Function is a type of function which has no name…
What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…