4/10 Days Of Javascript

4/10 Days Of Javascript

Welcome to the 4th day of JavaScript. On this day we are going to learn about ARRAYS. Open codepen.io and let's get started.

What are Arrays?

On day one we learnt how to store a value into a variable. Now, what if we want to store multiple values in the same variable?

Well, that's where arrays come into the picture. You can store multiple values in a single variable. An array is a collection of multiple values stored in the same variable. You can define an array by using square brackets, where each element is separated by a comma

//below is an example of an array
let myExample = ["this", "is", "example", "of", "array" ]
let myNumbers = [1, 2, 3, 4, 5, 6]

You can even include an object inside an array

let oBject = [ {name: "bike", wheels: 2},{name: "car", wheels: 4} ]

Adding elements to the existing array

We can access our array by typing out the name of the array followed by a period, exactly the same way we accessed an object. In order to add an element to an array, we need to use the push method right after accessing the array. We'll be accessing the objects and printing the outputs by using a special function console.log. Console.log is a function in javascript that is used to print any pre-defined variable. Let's add another number to array myNumbers and print it.

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

Learning this method is crucial as they make it a lot easier for a programmer to access the data without actually changing the initial code. You can access the contents of an array using typing the index number in the square brackets.

Importance of array

An array is a really important concept in any programming language. You see a computer is really good at performing an automated task. This is where an array is very useful. We can feed a lot of information into an array and then give that information to the computer to perform a certain task whether it is arranging the array in ascending or descending order or logically anything.

Did you find this article valuable?

Support Parikshit Hiwase by becoming a sponsor. Any amount is appreciated!