Array in JavaScript

Β·

3 min read

Hello Readers!!πŸ‘‹

I am Anshu Shree and an aspiring Fullstack developer. I am here to share my knowledge and experience of technology with you.

Today I am going to talk about Array in JavaScript. Let's start our journey!

Do you know what is array and why is it useful?

What is Array and why is it useful?

Definition: An array is a data structure that stores an ordered list of elements. It can hold elements like strings, numbers, objects and even other arrays.

In javascript, an array is a type of object. It has many inbuilt methods for manipulating arrays.

Why do we need it?

  1. grouping related data

  2. storing a large amount of data

  3. improving performance

  4. ease of use

  5. better readability

How do we declare an array?

There are three ways to do it.

  • using square braces []

      let fruits = ["Kiwi","Mango","Apple"];
    
  • using array constructor

let fruits = new Array("Kiwi","Mango","Apple");
let emptyArr = new Array(9);
  • using an array literal
let fruits = Array("Kiwi","Mango","Apple");

How to store and access elements in Array?

Here is an example of how we store elements in Array.

let players = [];
// storing elements in array
players[0] = "Virat";
players[1] = "Rohit"
players[2] = "MS Dhoni"

// print array after storing
console.log(players) // ["Virat","Rohit","MS Dhoni"]

Here is an example of how we access elements in Array.

let players = [ "Virat","Rohit","MS Dhoni"]

//print element of array on accessing
console.log(players[1]) // Rohit

Array Methods

However, there are methods in an array. But I have mentioned only those methods which are commonly used.

  1. pop: Removes the last element from an array and returns that element.
const fruits = ['apple', 'banana', 'orange'];
const lastFruit = fruits.pop();
// lastFruit is 'orange', fruits is now ['apple', 'banana']
  1. push: Adds one or more elements to the end of an array and returns the new length of the array.
const fruits = ['apple', 'banana'];
fruits.push('orange');
// fruits is now ['apple', 'banana', 'orange']
  1. concat: Combines two or more arrays and returns a new array.

     const array1 = [1, 2, 3];
     const array2 = [4, 5, 6];
     const combinedArray = array1.concat(array2);
     // combinedArray is [1, 2, 3, 4, 5, 6]
    
  2. join: Joins all elements of an array into a string, optionally using a specified separator.

const fruits = ['apple', 'banana', 'orange'];
const joinedString = fruits.join(', ');
// joinedString is "apple, banana, orange"
  1. slice: Returns a shallow copy of a portion of an array into a new array.
const fruits = ['apple', 'banana', 'orange', 'mango', 'kiwi'];
const slicedFruits = fruits.slice(1, 4);
// slicedFruits is ['banana', 'orange', 'mango']
  1. splice: Changes the contents of an array by removing or replacing existing elements and/or adding new elements.
const fruits = ['apple', 'banana', 'orange'];
const removedFruits = fruits.splice(1, 1, 'mango', 'kiwi');
// removedFruits is ['banana'], fruits is now ['apple', 'mango', 'kiwi', 'orange']

There are many methods like indexOf(), lastIndexOf(), find(), every() and many more. If you are interested in exploring many methods of the array. So go to the Mozilla docs.

Here I provide the link to the array methods of Mozilla Docs :

πŸ”— https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

So, I am here to end. Let's meetπŸ«‚ in the next blog. Thank you for visiting my blog page. I am always welcomed to your suggestions. Feel free to share and like my blog.

Follow me on :

LinkedIn: https://www.linkedin.com/in/anshushree/

Bye!πŸ™‹β€β™€οΈ

Β