A Complete Guide to Arrays in JavaScript

A Complete Guide to Arrays in JavaScript

Arrary start from "0" not "1"

ยท

3 min read

An array is a data structure which store multiple values(object , Boolean, etc) under a single variable. Each value is called an element specified by an index(i).

JavaScript Array Methods :

There are lot of array methods and all of them are important, but I will discuss a few methods.

Example of array


let array = [
  1,
  "I write code",
  true,
  techAvenger = {
    CEO: "Sudhanshu Kumar",
    CTO: "Hitesh Choudhary",
    CIO: "Krish Naik",
  },
  setvices = [
    "iNeuron",
    "LCO",
    "findcoder",
    "uiColorpicker",
    true,
    "I write code",
  ],
];

> Array store - Object , array , integer , string , Boolean atc.

length :

let countries = ["India", "New Zealand", "Brazil", " ๐Ÿ˜"];
console.log(countries.length);
> result: 4

The length property returns the number of elements in that array.

sort:

let alpha = ["d", "b", "a", "e", "c"];
alpha.sort()
> result: [ 'a', 'b', 'c', 'd', 'e' ]

The sort() method sorts an array alphabetically

pop:

const socialMedia = ["Instagram", "Linkedin", "Twitter", "Facebook"];
socialMedia.pop()
> result: [ 'Instagram', 'Linkedin', 'Twitter' ]

The pop() method removes the last element from an array . If you do colsole.log(socialMedia.pop()) then you get the removed value

push:

const emojis = ["๐Ÿ˜€", "๐Ÿ˜„", "๐Ÿ˜", "๐Ÿ˜†", "๐Ÿ˜‚"];
emojis.push("๐Ÿคฃ")
result: ["๐Ÿ˜€", "๐Ÿ˜„", "๐Ÿ˜", "๐Ÿ˜†", "๐Ÿ˜‚" ,"๐Ÿคฃ" ]

The push() method adds a new element to an array (at the end)

shift:

const emojis = ["remove","๐Ÿ˜€", "๐Ÿ˜„", "๐Ÿ˜", "๐Ÿ˜†", "๐Ÿ˜‚"];
emojis.shift()
result: ["๐Ÿ˜€", "๐Ÿ˜„", "๐Ÿ˜", "๐Ÿ˜†", "๐Ÿ˜‚" ]

The shift() method removes the first array element

unshift:

const emojis = ["๐Ÿ˜€", "๐Ÿ˜„", "๐Ÿ˜", "๐Ÿ˜†", "๐Ÿ˜‚"];
emojis.unshift("add me")
result: ['add me',"๐Ÿ˜€", "๐Ÿ˜„", "๐Ÿ˜", "๐Ÿ˜†", "๐Ÿ˜‚" ]

The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements

concat:

const india = [" Virat Kohli", " Rohit Sharma", " Hardik Pandya"];
const pakistan = ["Babar Azam", "Mohammad Wasim", "Hasan Ali"];
india.concat(pakistan)
result: [" Virat Kohli", " Rohit Sharma", " Hardik Pandya" , "Babar Azam", "Mohammad Wasim", "Hasan Ali"]

The concat() method creates a new array by merging existing arrays

split:

let name = "Deepak";
name.split("")
result: [ 'D', 'e', 'e', 'p', 'a', 'k' ]

splits a string into an array of substrings, and returns the array

splice:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
result: [ Banana,Orange,Lemon,Kiwi,Apple,Mango]

The first parameter (2) defines the position where new elements should be added (spliced in). The second parameter (0) defines how many elements should be removed. The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added. The splice() method returns an array with the deleted items:

##These are some JavaScript array methods. Hope you enjoy reading this article. If you like it, give it a hard and leave a comment so that I can be inspired and I can write articles like this

ย