Javascript Arrays Concat Join Reverse Slice Sort toString ValueOf Method


Array Objects

concat Method join Method reverse Method slice Method sort Method toString Method valueOf Method

Array Object

Description

Provides support for creation of arrays of any data type.

Syntax

new Array()
new Array(
size)
new Array(element0, element1, ..., elementn)

The Array object creation syntax has these parts:

Part Description
size The size of the array. As arrays are zero-based, created elements will have indexes from zero to size -1.
element0,...,elementn The elements to place in the array. This creates an array with n + 1 elements, and a length of n.
Remarks

After an array is created, the individual elements of the array can be accessed using [ ] notation, for example:

var my_array = new Array();
for (i = 0; i < 10; i++)
    {
    my_array[i] = i;
    }
x = my_array[4];

concat Method (Array)
Description

Returns an new array consisting of a combination of two arrays.

Syntax

array1.concat(array2)

The concat method syntax has these parts:

Part Description
array1 Required. An Array object to concatenate with array2.
array2 Required. An Array object to concatenate to the end of array1.
Remarks

The concat method returns an Array object containing the concatenation of array1 and array2.

If an object reference is copied from either array1 or array2 to the result, the object reference in the result still points to the same object. Changes to that object are reflected in both arrays.

The following example illustrates the use of the concat method:

function ConcatArrayDemo()
{
  var a, b, c;
  a = new Array(0,1,2,3,4);
  b = new Array(5,6,7,8,9);
  c = a.concat(b);
  return(c);
}

join Method
Description

Returns a String object consisting of all the elements of an array concatenated together.

Syntax

arrayobj.join(separator)

The separator argument is a String object that is used to separate one element of an array from the next in the resulting String object. If omitted, the array elements are separated with an empty string.

Remarks

The join method returns a String object that contains each element converted to a string and concatenated together.

The following example illustrates the use of the join method:

function JoinDemo()
{
  var a, b;
  a = new Array(0,1,2,3,4);
  b = a.join("-");
  return(b);
}

reverse Method
Description

Returns an Array object with the elements reversed.

Syntax

arrayobj.reverse( )

Remarks

The reverse method reverses the elements of an Array object in place. It does not create a new Array object during execution.

If the array is not contiguous, the reverse method creates elements in the array that fill the gaps in the array. Each of these created elements has the value undefined.

The following example illustrates the use of the reverse method:

function ReverseDemo()
{
  var a, l;
  a = new Array(0,1,2,3,4);
  l = a.reverse();
  return(l);
}

slice Method (Array)
Description

Returns a section of an array.

Syntax

arrayObj.slice(start, [end])

The slice method syntax has these parts:

Part Description
arrayObj Required. An Array object.
start Required. The zero-based index of the beginning of the specified portion of arrayObj.
end Optional. The zero-based index of the end of the specified portion of arrayObj.
Remarks

The slice method returns an Array object containing the specified portion of arrayObj.

The slice method copies up to, but not including, the element indicated by end. If negative, end indicates an offset from the end of arrayObj. In addition, it is not zero-based. If omitted, extraction continues to the end of arrayObj.

In the following example, all but the last element of myArray is copied into newArray:

newArray = myArray.slice(0, -1)

If an object reference is copied from arrayObj to the result, the object reference in the result still points to the same object. Changes to that object are reflected in both arrays.

function sliceD()
{
var a, b, c;
a = new Array(0,1,2,3,4);
b = a.
slice(0,-1);
alert(b);
}


sort Method
Description

Returns an Array object with the elements sorted.

Syntax

arrayobj.sort(sortfunction)

The sortfunction argument is the name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.

Remarks

The sort method sorts the Array object in place; no new Array object is created during execution.

If you supply a function in the sortfunction argument, it must return one of the following values:

  • A negative value if the first argument passed is less than the second argument.
  • Zero if the two arguments are equivalent.
  • A positive value if the first argument is greater than the second argument.

The following example illustrates the use of the sort method:

function SortDemo()
{
  var a, l;
  a = new Array("X" ,"y" ,"d", "Z", "k","m","r");
  l = a.sort();
  alert(l);
}

toString Method
Description

Returns a string representation of an object.

Syntax

objectname.toString([radix])

The toString method syntax has these parts:

Part Description
objectname Required. An object for which a string representation is sought.
radix Optional. Specifies a radix for converting numeric values to strings.
Remarks

The toString method is a member of all built-in JScript objects. How it behaves depends on the object type:

Object Behavior
Array Elements of an Array are converted to strings. The resulting strings are concatenated, separated by commas.
Boolean If the Boolean value is true, returns "true". Otherwise, returns "false"
Function Returns a string returned of the following form, where functionname is the name of the function whose toString method was called:
function functionname( ) { [native code] }
Number Returns the textual representation of the number.
String Returns the value of the String object.
Default Returns "[object objectname]", where objectname is the name of the object type.

The following example illustrates the use of the toString method with a radix argument:

function CreateRadixTable ()
{
  var s1, s2, s3, x;
  document.write("Hex    Dec   Bin<BR>");
  for (x = 0; x < 16; x++)
  {
    switch(x)
    {
      case 0 :
        s1 = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
        s2 = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
        s3 = '&nbsp;&nbsp;&nbsp;&nbsp;';
        break;
      case 1 :
        s1 = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';;
        s2 = '&nbsp;&nbsp;&nbsp;&nbsp;';
        s3 = '&nbsp;&nbsp;&nbsp;';
        break;
      case 2 :
        s3 = '&nbsp;&nbsp;';
        break;
      case 3 :
        s3 = '&nbsp;&nbsp;';
        break;
      case 4 :
        s3 = '&nbsp;';
        break;
      case 5 :
        s3 = '&nbsp;';
        break;
      case 6 :
        s3 = '&nbsp;';
        break;
      case 7 :
        s3 = '&nbsp;';
        break;
      case 8 :
        s3 = "" ;
        break;
      case 9 :
        s3 = "";
        break;
      default:
        s1 = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
        s2 = "";
        s3 = '&nbsp;&nbsp;&nbsp;&nbsp;';
    }
    document.write(" ", x.toString(16), s1, x.toString(10), s2, s3, x.toString(2), "<BR>");
  }
}

valueOf Method


Description

Returns the primitive value of the specified object.

Syntax

object.valueOf( )

The object argument is any JScript object.

Remarks

The valueOf method is defined differently for each intrinsic JScript object.

Object Return Value
Array The elements of the array are converted into strings, and the strings are concatenated together, separated by commas. This behaves the same as the Array.toString and Array.join methods.
Boolean The Boolean value.
Date The stored time value in milliseconds since midnight, January 1, 1970 UTC.
Function The function itself.
Number The numeric value.
Object The object itself. This is the default.
String The string value.

The Math object does not have a valueOf method.