1.

JavaScript Arrays

Answer»

Arrays are the next item on our JavaScript cheat sheet. Arrays are used in a variety of programming languages. They are a method of categorising variables and attributes. Arrays can be defined as a collection of objects of the same type. In JavaScript, here's how one can make an array of cars:

var cars = ["Mercedes", "Tesla","Volvo"];

Now that we understand how to make arrays, we can perform a bunch of operations on them. Let us take a look at some JavaScript methods which can be used to perform various types of operations on arrays:



  • pop(): This method is used for removing the last element of an array.


  • push(): This method is used for adding a new element at the very end of an array.


  • concat(): This method is used for joining various arrays into a single array.


  • reverse(): This method is used for reversing the order of the elements in an array.


  • shift(): This method is used for removing the first element of an array.


  • slice(): This method is used for pulling a copy of a part of an array into a new array.


  • splice(): This method is used for adding elements in a particular way and position.


  • toString(): This method is used for converting the array elements into strings.


  • unshift(): This method is used for adding new elements at the beginning of the array.


  • valueOf(): This method is used for returning the primitive value of the given object.


  • indexOf(): This method is used for returning the first index at which a given element is found in an array.


  • lastIndexOf(): This method is used for returning the final index at which a given element appears in an array.


  • join(): This method is used for combining elements of an array into one single string and then returning it.


  • sort(): This method is used for sorting the array elements based on some condition.




Discussion

No Comment Found