Table of contents
Hey Coders!!π€ So, this is my Part-5 of Javascript for beginners!!π©βπ»
So, if you have not seen my Part-1, Part-2, Part-3 and Part-4 then you must see them and understand them because in the 4 articles we have talked much more!!
So, without any further do let's get started!π
Let's go!!
Store multiple things with Arrays!πΌ
So, in Javascript if you wanna store multiple things or elements then you have to use Arrays
!
So, if you have already learned Python basics then you must have the knowledge of strings right? But if you don't know what are lists in Python
LET"S SEE!!π€
IN PYTHON:
sports_names= ["Swimming", "Badminton", "Cricket", "Football",]
print(sports_names)
OUTPUT:
Similarly, in Javascript there are Arrays
where you can store as many elements as you want!!
EXAMPLE IN JAVASCRIPT
var sports_names = ['Swimming', 'Badminton', 'Cricket', 'Football']
console.log(sports_names)
OUTPUT:
Nested Arrays in JS!π‘
Example:
var subjects = [['Maths', 'English'], ['Science', 'History']]
console.log(subjects)
Output:
EXPLAINATION: When one of the elements in an array is another array that's called a nested array or a multidimensional array!! You can see there's the beginning of the array and there's the end of the array. But the first element in this array is another array with two elements of it's own, similarly with the second array!!π
Access array with data indexes!π§Ά
Earlier we learned how to use bracket notation to find a specific index in strings. you can do same things with arrays also!! Let's seeπ
var myArray = ['Swimming', 'Badminton', 'Cricket']
var myData = myArray[0]
console.log(myData)
Output:
Modify Array data with Indexes!π«
You can use array indexes to modify arrays. Now we tried to do this earlier using bracket notations and we were not able to modify a string using bracket notation But with arrays you can!! LIKE THISπ
var myArray = [100, 200, 300, 400]
var myData = myArray[2] = 500
console.log(myArray)
Output:
Access Multi-Dimensional arrays!!πͺ
var myArray = [[1, 2, 3], [4, 5, 6], [7,8,9], [10,11,12]]
var myData = myArray[0][0]
console.log(myData)
Output:
Explanation:
- So, here we're having at the very first is a outer Array and inside this outer array there are many elements, like[1, 2, 3]and so on...
- I have made one more variable which is
myData
inside of it we're having index[0]
which means what that the first array will be counted which is[1, 2, 3]
and after this we're having one more[0]
which means that inside of the[1, 2, 3]
the 1st position we be counted which is1
for that reason we're getting1
as a output!!π
Similarly, if we want an output as 10
then we will do LIKE THIS:π
var myArray = [[1, 2, 3], [4, 5, 6], [7,8,9], [10,11,12]]
var myData = myArray[3][0]
console.log(myData)
Output:
Manipulate Arrays with push()π£
From the push()
element we can add anything we want to!! LIKE THISπ
Example:
var basket = ["Mango", "Banana", "Orange"]
basket.push(["Litchi", "Grapes"])
console.log(basket)
Output:
Manipulate Arrays with pop()π
Basically pop()
is an element which helps to remove the last array.
Example:
var myArray = ["Apple", "Banana", "Grapes"]
myArray.pop()
console.log(myArray)
Output:
πHere, we can see that the last array was Grapes
so pop()
removed it!!
Next Example:
var myArray = [["Mango", "Litchi"] , ["Pineapple", "Custard apple"]]
var ourArray = myArray.pop()
console.log(ourArray)
Output:
- Here above we can see that we're having
2 arrays
insidemyArray
Now, in the second var
ourArray
we're havingmyArray.pop()
which means it'll remove the last one!! And the last one is ["Pineapple", "Custard apple"] so, this will stored in ourArray ok?After running we're getting ["Pineapple", "Custard apple"] because we are running
ourArray
.Let's run myArray!!
var myArray = [["Mango", "Litchi"] , ["Pineapple", "Custard apple"]]
var ourArray = myArray.pop()
console.log(myArray)
Output:
Hopefully that went well!!π
Manipulate arrays with shift()πΉ
Basically shift()
operator removes the first array among the following!!
var myArray = [["Mango", "Litchi"] , ["Pineapple", "Custard apple"]]
var ourArray = myArray.shift()
console.log(myArray)
Output:
Manipulate arrays with unshift()π₯
So, unshift()
is function almost same like push()
.
Basically, in push() function we can append at the end of the arrays. But in unshift() we can append at the very first of the arrays!
var amena = ["Studies", "Plays", "Codes", "Prays"]
amena.shift()
amena.unshift(["Drinks_Water"])
console.log(amena)
Output:
Hopefully you are done with the arrays now let's move on!!!π
Shopping list[]π
So, If your mom gives you a Grocery Shopping List then you must buy those things!! But Today we'll do the same thing in JS by making a shopping list!!π
Example:
var shoppingList = [["Apple", 6], ["Bananas", 6], ["Chips", 3], ["Chocolate", 2], ["Milk" , 2], ["cereal", 3], ["Bread", 1]]
console.log(shoppingList)
Output:
Hopefully you all are done with the arrays
part!!π
And if you're having any doubt please mention in the comments below!π¨βπ»
Motivation for today: βYou can get everything in life you want if you will just help enough other people get what they want.β
So, this was my Part -5 for JS for beginners, Now we'll come to the 6th part till then GOODBYE!π