Friday, July 12, 2013

JavaScript: More on Arrays

So what else can you do with JavaScript Arrays? Arrays are like a swiss army knife, you can use it like an array, you can use it like an object, you can use it like a stack and like a collection, whatever suits your purposes. Arrays have the following functions available
  • length - return the total number of elements in the array
  • join - concatenates all the elements in the array into a single string separated by the parameter passed
  • concat - concatenates an array to the array and returns a new array
  • sort - sorts the array by character encoding by default or by a user defined function
  • reverse - reverses the elements of the array
  • slice - returns an array that is a subset of the original array
  • splice - can insert, remove or replace one or more array elements
  • push - adds an element to the end of an array
  • pop - it removes and returns the last element of an array
  • shift - it removes and returns the first element of the array shifting all other elements down
  • unshift - inserts elements at the beginning of an array shifting all the other element up
  • toString - converts all the elements to a string and concatenates them to a comma delimited string
  • toLocaleString - converts all the elements to a localised string and concatenates them to a comma delimited string
Pretty flexible stuff, next we will look at examples of each of these functions


No comments: