Array Methods
Javascript defines a number of useful array manipulation functions on prototype, which means that they are available as methods of any These Javascript methods are introduced in the below:
join()
The join() method converts all the elements of an array to strings and concatenates them.
Here are some examples:
var a = [1, 2, 3]; // Create a new array with these three elements
a.join(); // => "1,2,3"
a.join(" "); // => "1 2 3"
a.join(""); // => "123"
a; // => [1, 2, 3] now a is in orignal form
reverse()
The reverse() method reverses the order of the elements of an array and returns the reversed
Here are some examples:
var a = [1,2,3]; // Create a new array with these three elements
a.reverse(); // => [3,2,1]
a; // => [3,2,1]
sort()
The sort() method sorts the elements of an array and returns the sorted When
sort() is called with no arguments, it sorts the array elements in Alphabetical order.
Here are some examples:
var a = [2,3,1]; // Create a new array
a.sort(); // => [1,2,3]
var a = ["banana", "cherry", "apple"]; // Create a new array
a.sort(); // => ["apple", "banana", "cherry"]
concat()
The concat() method creates and returns a new array that contains the elements of the original array on which concat() was invoked, followed by each of the arguments to concat().
Here are some examples:
var a = [1,2,3]; // Create a new array
a.concat(4, 5) // => [1,2,3,4,5]
a.concat([4,5]); // => [1,2,3,4,5]
a.concat([4,5],[6,7]) // => [1,2,3,4,5,6,7]
a.concat(4, [5,[6,7]]) // => [1,2,3,4,5,[6,7]]
slice()
The slice() method returns a slice, or subarray, of the specified Its two arguments specify the start and end of the slice to be returned. An argument of -1, specifies the last element in the An argument of -3, specifies the third last element in the
Here are some examples:
var a = [1,2,3,4,5]; // Create a new array
a.slice(0,3); // => [1,2,3]
a.slice(3); // => [4,5]
a.slice(1,-1); // => [2,3,4]
a.slice(-3,-2); // => [3]
splice()
The splice() method for inserting or removing
elements from an It can delete elements from an array, insert new elements into an array, or perform
both operations at the same time.
Here are some examples:
var a = [1,2,3,4,5,6,7,8]; // Create a new array
a.splice(4); // => delete elements from index 4 to at last [5,6,7,8] and a is [1,2,3,4]
a.splice(1,2); // => delete elements from index 1 to 2 [2,3]; a is [1,4]
a.splice(1,1); // => delete elements from index 1 to 1 [4]; a is [1]
var a = [1,2,3,4,5]; // Create a new array
a.splice(2,0,'a','b'); // => []; a is [1,2,'a','b',3,4,5]
a.splice(2,2,[1,2],3); // => ['a','b']; a is [1,2,[1,2],3,3,4,5]
push()
The push() method appends one or more new elements to the end of an array and returns the new length of the array.
Here are some examples:
var a = []; // Create a new array
a.push(1,2); // => 2 and a is[1,2]
a.push(3,4); // => 4 and a is [1,2,3,4]
pop()
The pop() method deletes the last element of an array, decrements the array length, and returns the value that it
removed.
Here are some examples:
var a = [1,2,3,4]; // Create a new array
a.pop(); // => 4 and a is[1,2,3]
a.pop(); // => 3 and a is [1,2,]
unshift()
The unshift() method adds an element or elements to the beginning of the array, shifts the existing array elements up to higher indexes to make room, and returns the new length of the array.
Here are some examples:
var a = []; // Create a new array
a.unshift(1); // => 1 and a is [1]
a.unshift(22); // => 2 and a is [22,1]
shift()
The shift() method removes and returns the first element of the array, shifting all subsequent elements down one place to occupy the newly vacant space at the start of the array.
Here are some examples:
var a = [1,2,3]; // Create a new array
a.shift(); // => 1 and a is [2,3]
a.shift(); // => 2 and a is [3]
toString()
The toString() method converts each of its elements to a string and outputs a comma-separated list of those strings.
Here are some examples:
[1,2,3].toString() // => '1,2,3'
["a", "b", "c"].toString() // => 'a,b,c'
[1, [2,'c']].toString() // => '1,2,c'