Blog

Array functions toString, join, pop, push, shift and unshift

The toString function converts an array to a string by concatenation elements with commas in between.
toString method

Example 1:
var a=Array(1,2,3);
var s=a.toString();
s is “1,2,3”

Example 2:
var a = Array(1, “apple”, Math,Math.sqrt);
var s = a.toString();
s is “1,apple,[object Math],function sqrt() { [native code] }”

join method
The join method provides the option to provide the connector between elements.
Example 1
var a = Array(1,2,3,4);
var s = a.join(“+”);
s is “1+2+3+4”

Example 2
var a = Array(1,2,3,4);
var s = a.join(Math);
s is “1[object Math]2[object Math]3[object Math]4”

pop method
This method removes the last element in the array and returns it.
Example 1
var a = Array(1,2,3,4);
var s = a.pop();
a is “1,2,3”
and
s is 4

push method
This method adds a new element to the end of the array and returns the new length of the array.

var a = Array(1,2,3,4);
var s = a.push(7);
a is 1,2,3,4,7

and
s is 5

shift method
The shift method removes the first element in the array instead of the last as in pop.
Example 1
var a = Array(1,2,3,4);
var s = a.shift();
a is 2,3,4

and
s is 1

unshift method
This method is the reverse of the push method. It adds an element at the beginning of the array.
Example 1
var a = Array(1,2,3,4);
var s = a.unshift(10);
a is 10,1,2,3,4

and 
s is 5