
What Arrays are why we need them.
Before moving further, let's first understand what problem arrays solve and why we need arrays.
Problem Statement :-
So the problem is this , imagine you are writing code, so basically you are building a school application software. In every class there is a certain number of students, and along with that each student also has their school personal details.
Now, will you store every student detail one by one in separate variables in your program ? if you do that, there will be too many variables in memory, and the memory usage will increase a lot.
Along with that, the readability of the code will also not be good. Now think about it , if there are 20 students, will you define every student's details one by one in different variables? this will make the code very polluted and messy.
And this example is only about 20 students. Now assume you have a much larger amount of data. So this becomes a very big problem.
Solution :-
To solve this problem, we use arrays. An array is a data structure, which means it is used to store data. In an array, you can store multiple values in a single variable. you can add n number of values in it.
Whatever separate data you have, you can put it inside an array, and whenever you need it, you can apply arrays methods on that array and get the data according to your need.
Below i will show you how you can use arrays with an example, so you can clearly understand how arrays are used and what problem they solve.
// Without array .
const stud1 = {name: "Nikhil", course: "BSC"};
const stud2 = {name: "Piyush", course: "CSE"};
const stud3 = {name: "Hitesh", course: "EE"};
// With array .
const student = [
{name: "Nikhil", course: "BSC"},
{name: "Piyush", course: "CSE"},
{name: "Hitesh", course: "EE"}
];
How to create an array.
There are many ways to create an array. Let's look at each method one by one .
The First way to create an array is by using array literals. Whatever data you have, you can put it inside the literal, and it will become an array. If you want to take data from it and perform some operations, you can simply use array methods on it.
Below is an example of how an array can be declared using literals.
const literal = [1,3,6,9]; console.log(literal);
The Second way it by using the new keyword with the Array constructor. You pass values as parameters, and it will put the those values into the array. But in the end, it still returns the array in literal form.
Below is an example of how an array can be declared using literals.
const arr = new Array(1,3,6,9): console.log(arr);
Accessing elements using index.
Before accessing an array, let's first understand how an array stores each element uniquely, meaning how indexing works in an array. once you understand indexing , it becomes easier to access array elements. There are several ways to access elements in an array. Below i have created a visual so you can understand it.
// Index 0 1 2 3 4
const arr = ['a', 'b', 'c', 'd', 'e'];
// Element 1 2 3 4 5
// Hope you understand it .
The index of an array always starts from , after that it increases by one for each element.
Whenever we want to get a value from the array, we use its index to access that element.
Now let's see how we can access elements in an array.
The first way is , if you already know which value you want to access, you can directly use the index of that element and declare it to use it. That's it, you will get the value.
let arr = [1,2,3,4]; console.log(arr[0]); // Print 1 // Because 1 have index 0.
The Second way it , if you know the index, you can store that index in a variable and then use that variable to access the value.
let arr = ['A','B','C']; let first = arr[0]; console.log(first);
You can also dynamically add values in a literal and then access them.
- Also there are lot's of methods and ways to access the array element according to need.
Update Elements.
If you have an array and you want to update one element from it, you can easily do it. Now already know about indexing and how to access an element using an index, so updating it also becomes easy.
Below is a code example , you can see that , it is very simple way to do it.
let a = ["Hello", "Hi", "Bye"];
console.log(a);
a[1]= "Good";
console.log(a);
// So this how you can update the array element.
Array length properties.
The array length property basically return the number of elements present inside the array. If your array has 10 elements, then its length will be 10. If it has 20 elements, then its length will be 20.
To check the length of an array, we have a property called length. By applying this property, we can see how many elements are present in the array. Below is a code example you can see.
let a = [1,2,3,4,5,6,7,8,9,10];
let lenth = a.length;
console.log("Array Length: " + lenth);
Basic looping over arrays.
Basic looping over arrays means printing all the values inside the array one by one. In the problem statement, we saw that an array can store data as needed.
Now, to use each piece of data, we use a loop because it goes to every element and perform according to conditions.
let num = [5,10,15,20,25,30];
for(let i = 0; i < num.length; i++) {
console.log(num[i]);
}




