Arreeh to Array

Array can be imagined as a continuous block of memory.

In JavaScript Array is a data structure that allows to store and organize multiple values within a single variable. It is a versatile and dynamic object. It can hold various data types, including numbers, strings, objects, and even other arrays. Arrays in JavaScript are zero-indexed i.e. the first element is accessed with an index 0, the second element with an index of 1 and so on .

creation of Array

Arrays JavaScript can be created with Array constructor or array literals( [ ] ).we can create array like this

let studentName = new Array('jeet', 'raj', 'sourav');
//or simply can be created using array literals
let girlName = ['preeti', 'ruhi', 'antara'];
//or we can also create an empty array like this
let arr = [];
//these arrays can easilly acessed and also modifiedon the basis
// of index no like  :
girlName[1] = 'sneha';
//Here index 1 element(ruhi) is going to be changed

Traversal in array

Arrays can be traversed using various loops and methods .lets discuss about them one by one.

let names = ['jeet', 'subhman', 'isaan', 'nitish'];
//for of loop
//for...of loop is used to iterate over an iterable object
//such as array strings and other iterable object like NodeList etc
for (let item of names) {
  console.log(item);
}
//also can be implemented like this 
 for (let item = 0; item < names.length; item++) {
  console.log(fruits[item]);

//for...in Loop: The for...in loop is used to iterate over the
//properties (including indices) of an object.
for(let item in names){
console.log(item);
}

both for...of and for...in loops iterates over something but the key difference is on what they iterate over.

The for...in statement iterates over the enumerable string properties of an object, while the for...of statement iterates over values that the iterable object defines to be iterated over. we will discuss it on the series of objects.

We can also do the same task with the help of 'forEach' method and 'map' function.

arr.forEach is a method which calls the provided function once for each element of the array. The provided function may perform any kind of operation of the elements of the given array although this method can not return a value.

on the other hand map function creates a new array from calling a function for every element. It doesn't change the original array.

let names = ["jeet", "raj", "payel", "ishan", "anuska"];

names.forEach((currElem, index, arr)=>{
console.log(`${currElem} is at index ${index}`);
//console.log(arr);
});
//forEach method is used when we just have to manipulate and play 
//with the array
const myMapArr = names.map((curElem, index, arr) => {
 return `Element is ${curElem} `;
});

console.log(myMapArr);//results a new array containing the reasult 
                      //after applying the function on each element
console.log(names);//it doesnt change the original array

forEach is used when we just have to iterate over the array and perform some task and since map returns a new array we can chain other array methods with it.

This is not the end , see you in the next one.

0
Subscribe to my newsletter

Read articles from Jeet kangsabanik directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jeet kangsabanik
Jeet kangsabanik

I am jeet , experienced full-stack developer proficient in HTML, CSS, JavaScript, React, MongoDB, MySQL, and Express.js. Dedicated to creating innovative and robust web applications with a keen eye for design and a commitment to delivering high-quality, scalable solutions that meet and exceed client expectations.