Skip to content

Latest commit

 

History

History
119 lines (86 loc) · 2.81 KB

004-array-methods-in-javascript.md

File metadata and controls

119 lines (86 loc) · 2.81 KB
id title tags author meta-description date keywords template categories image
004-array-methods-in-javascript
Array methods in JavaScript
javascript
es6
hacktoberfest
beginner
Anshu Toppo
Array methods in JavaScript
2020-10-19 12:58:58 +0530
javascript, es6, hacktoberfest, beginner
post
javascript
assets/images/javascript/array.svg

Arrays

An array is a special variable, which can hold more than one value at a time.

Syntax to create an Array .

 var array_name = [item1, item2, ...];
 var cars = ["Audi", "Volvo", "BMW"];
 console.log(cars); // Result : ["Audi","Volvo","BMW"]
  1. toString() method

    To convert Array to String.

     var cars = ["Audi", "Volvo", "BMW"];
     console.log(cars.toString());
     // Result : Audi, Volvo, BMW
  2. push() and pop() method

    The push() method adds a new element to an array.

    var cars = ["Audi", "Volvo", "BMW"];
    cars.pop());
    console.log(cars);
    // Result : ["Audi", "Volvo", "Merc", "Merc"]

    The pop() method removes the last element from an array,

    var cars = ["Audi", "Volvo", "BMW"];
    console.log(cars.pop());
     // Result : BMW
  3. shift() method and unshift() method

    The shift() method removes the first array element and "shifts" all other elements to a lower index

    var cars = ["Audi", "Volvo", "BMW"];
    console.log(cars.shift())
    // Result : Audi

    The unshift() method adds a new element to an array (at the beginning)

    var cars = ["Audi", "Volvo", "BMW"];
    cars.unshift("Jaguar");
    console.log(cars);
     // Result : ["Jaguar", "Audi", "Volvo", "BMW"]
  4. splice() method

    The splice() method can be used to add new items to an array

    var cars = ["Audi", "Volvo", "BMW"];
    cars.splice(2,0,"Jaguar","Accent");
    console.log(cars);
    // Result : ["Audi", "Volvo", "Jaguar", "Accent", "BMW"]

    The first parameter (2) defines the position where new elements should be added The rest of the parameters ("Volvo" , "Accent") define the new elements to be added.

  5. concat() method

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

    var cars = ["Audi", "Volvo", "BMW"];
    var bikes =["Yamaha", "KTM"]
    var vehicles = cars.concat(bikes);
    console.log(vehicles);
    // Result : ["Audi", "Volvo", "BMW", "Yamaha", "KTM"]
  6. slice() method

    The slice() method slices out a piece of an array into a new array. it takes two args slice(start , upto)

    var vehicles = ["Audi", "Volvo", "BMW", "Yamaha", "KTM"]
    var onlyBikes = vehicles.slice(3)
    console.log(onlyBikes) // Result : ["Yamaha", "KTM"]