Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Array Methods

JS Array Methods

JavaScript array methods are used to perform various operations on array.

  • Array length
  • Array toString()
  • Array pop()
  • Array push()
  • Array shift()
  • Array unshift()
  • Array join()
  • Array delete()
  • Array concat()
  • Array flat()
  • Array splice()
  • Array slice()
Array length

The length property returns the length (size) of an array.

					 
        
          const services = ["Website", "Applications", "Software"];
          services.length;  // 3

          // access first element
          services[0]; // Website

          // access first element
          services[services.length - 1]; // Software
        
      
Array toString()

The toString() method converts an array to a string of (comma separated) array values.

					 
        
          const services = ["Website", "Applications", "Software"];
          services.toString();  // Website,Applications,Software
        
      
Array pop()

The pop() method removes the last element from an array and returns the value that was "popped out"

					 
        
          const services = ["Website", "Applications", "Software"];
          services.pop(); // Software
          services.toString();  // Website,Applications
        
      
Array push()

The push() method adds a new element to an array (at the end) and returns the new array length.

					 
        
          const services = ["Website", "Applications", "Software"];
          services.push('Design'); // 4
          services.toString();  // Website,Applications,Software,Design
        
      
Array shift()

The shift() method removes the first array element and "shifts" all other elements to a lower index and returns the value that was "shifted out".

					 
        
          const services = ["Website", "Applications", "Software"];
          services.shift(); // Website
          services.toString();  // Applications,Software,Design
        
      
Array unshift()

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

					 
        
          const services = ["Website", "Applications", "Software"];
          services.unshift('Design'); // 4
          services.toString();  // Design,Website,Applications,Software
        
      
Array join()

The join() method also joins all array elements into a string. It behaves just like toString(), but in addition you can specify the separator.

					 
        
          const services = ["Website", "Applications", "Software"];
          services.join('*'); // Website*Applications*Software
        
      
Array concat()

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

The concat() method does not change the existing arrays. It always returns a new array.

					 
        
          const services1 = ["Website", "Applications", "Software"];
          const services2 = ["Design", "Marketing"];
          const services3 = ["Advertisement"];

          const services = services1.concat(services2, services3);
          services.toString() // ["Website", "Applications", "Software", "Design", "Marketing", "Advertisement"]

          const newArr = services1.concat("Consulting");
          newArr.toString() // ["Website", "Applications", "Software", "Consulting"]
        
      
Array flat()

Flattening an array is the process of reducing the dimensionality of an array.

The flat() method creates a new array with sub-array elements concatenated to a specified depth.

					 
        
          const myArr = [[1,2],[3,4],[5,6]];
          const newArr = myArr.flat(); // [1,2,3,4,5,6]
        
      
Array splice()

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

The first parameter (1) 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 ("Design", "Marketing") define the new elements to be added.

The splice() method returns an array with the deleted items

					 
        
          const services1 = ["Website", "Applications", "Software"];
          services1.splice(1, 0, "Design", "Marketing"); // null
          services1.toString() // Website,Design,Marketing,Applications,Software

          const services2 = ["Website", "Applications", "Software"];
          services2.splice(1, 1, "Design", "Marketing"); // ["Applications"]
          services2.toString() // Website,Design,Marketing,Software

          const services3 = ["Website", "Applications", "Software"];
          services3.splice(1, 1); // ["Applications"]
          services3.toString() // Website,Software
        
      
Array slice()

The slice() method slices out a piece of an array into a new array.

The slice() method creates a new array.

The slice() method does not remove any elements from the source array.

The slice() method can take two argument.

The method then selects elements from the start argument, and up to (but not including) the end argument.

					 
        
          const services = ["Website", "Applications", "Software", "Design", "Marketing"];
          services.slice(1, 3); // ["Applications", "Software"]
        
      
Array delete()

Array elements can be deleted using the JavaScript operator delete.

Using delete leaves undefined holes in the array

Use pop() or shift() instead.

					 
        
          const services = ["Website", "Applications", "Software"];
          delete services[1];
          services[1]; // undefined
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved