JavaScript for Beginners!   Part-5πŸ•”

JavaScript for Beginners! Part-5πŸ•”

Β·

4 min read

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:

image.png

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:

image.png

Nested Arrays in JS!🎑

Example:

var subjects = [['Maths', 'English'], ['Science', 'History']]
console.log(subjects)

Output:

image.png

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:

image.png

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:

image.png

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:

image.png

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 is 1 for that reason we're getting 1 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:

image.png

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:

image.png

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:

image.png

πŸ‘†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:

image.png

  • Here above we can see that we're having 2 arrays inside myArray
  • Now, in the second var ourArray we're having myArray.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:

image.png

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:

image.png

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: image.png

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:

image.png

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!πŸ‘‹

Β