World of JavaScript Array

World of JavaScript Array

Let's deep dive into JavaScript Array

Why array is even required, we have already different datatypes through which can store in value inside that.

Let me answer this question,

When we want to store list of elements, it's become very tough for us to think name different for each variable to overcome this problem arrays are introduced in javaScript.

What is JavaScript Arrays?

In JavaScript, Array is a collection of values they may be different datatype, but in some other language they must have same data type i.e. C++, Java ,etc.

In JavaScript array is defined using [] i.e. [25,12,"lco",true]

In programming Sequence in start from 0, which is also known as index's.

It's deal same way as you with any other data type in JavaScript. The difference is that we can access each value inside the list individually, and perform various activities, such as looping over it.

Let's understand with an example.

let student =["Anup Maurya", "Adarsh Pal", "Atul Kashyap", "Abhishek Bhardwaj "];
for (let x in student) {
  console.log(student[x]); 
}

JavaScript Array Operations

  • Creation of Arrays

The items in an array are enclosed within square brackets.

let example= [25,12,"Anup Maurya",true];
  • Accessing Array Elements

For accessing the elements inside an array is also square brackets. Every element in the array is assigned with an index. By default, the first index of an array is zero. To retrieve a specified element from an array, a pair of square brackets enclosing the index value is used, i.e., example[0].

let example= [25,12,"Anup Maurya",true];

console.log(example[0]); //25

console.log(example[2]);  //Anup Maurya
  • Array Length Property

The length property retrieves the length of the array. Let's see

let example= [25,12,"Anup Maurya",true];

let len = example.length;

console.log(len); //4
  • Accessing the Last Array Element

Arrays use zero-based indexing, the index of the last element is one minus the length. Let's understand with an example.

let example= [25,12,"Anup Maurya",true];

let last = example[example.length - 1];

console.log(last);
  • Loop Over the Array Items

You can also loop over each element of array at once. We use the forEach method for this, which calls a function once for each element in an array.


example.forEach((item, index, array) => {

        console.log(item, index);

      });

The code above displays the name of the element and their corresponding indices on the console.

That's all for this article and with that, it's a wrap! I hope you found the article useful. Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!

I create content about Programming, Design, and Technology, If this is something that interests you, please share the article with your friends and connections. You can also subscribe to my newsletter to get updates every time I write something!

Ta da! Until we meet again Happy Coding! ❤️

Did you find this article valuable?

Support Anup Kumar Maurya by becoming a sponsor. Any amount is appreciated!