0% found this document useful (0 votes)
29 views

The Arrays Object

The document discusses arrays in JavaScript. It describes how to create arrays using literals or constructors, and lists common array properties like length and methods like push(), pop(), slice() etc. It also provides examples of storing different data types in arrays and creating multidimensional arrays.

Uploaded by

Ashokkumar A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

The Arrays Object

The document discusses arrays in JavaScript. It describes how to create arrays using literals or constructors, and lists common array properties like length and methods like push(), pop(), slice() etc. It also provides examples of storing different data types in arrays and creating multidimensional arrays.

Uploaded by

Ashokkumar A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 7

The Arrays Object

The Array object let's you store multiple values in a single variable.
Syntax:
Creating a Array object:
var ArrayVar = new Array( length);
The Array parameter is a list of strings or integers. When you specify
a single numeric parameter with the Array constructor, you
specify the initial length of the array. The maximum length
allowed for an array is 4,294,967,295.
The Array constructor syntax has three different forms. If you call
the constructor with two or more arguments, the arguments
initialize the array elements. If you only supply one argument to
the Array constructor, the argument initializes the length of the
new array; the new array’s elements are not initialized. Finally, if
you call the constructor without arguments, the new array’s
length is set to zero, and its elements are not initialized.
Here are examples:
var myArray4 = new Array(1,3,5,7,9) // an array with 5 elements
var myArray5 = new Array(100) // an empty array of length 100
var myArray6 = new Array() // an empty array of length 0
You can create an array using either an array initializer (array literal) or
the Array constructor.
The array initializer (array literal) syntax is simple: a comma-separated list of
values in square brackets. Here are some examples:
var myArray1 = [1,3,5,7,9] // an array with 5 elements
var myArray2 = [5] // an array with 1 element
An array can store anything you can assign to a variable: booleans, numbers,
strings, functions, objects, other Arrays, even regular expressions…
var myArray = [ 3, 'hello!', function() {return 5}, {'color':'blue', 'budget':25},
/[ell]/i ];
document.writeln('0>'+myArray[0]+'<BR>'); // Will output: 0>3
document.writeln('1>'+myArray[1]+'<BR>'); // Will output: 1>hello!
document.writeln('2>'+myArray[2]()+'<BR>'); // Will output: 2>5
document.writeln('3>'+myArray[3].color+'<BR>'); // Will output: 3>blue
document.writeln('3>'+myArray[3].budget+'<BR>'); // Will output: 3>25
document.writeln('4>'+myArray[4].test(myArray[1])+'<BR>'); // Will output:
4>true
JavaScript does not have a special syntax for creating
multidimensional arrays. A common workaround is to create
an array of arrays in nested loops.
The following code example illustrates the array-of-arrays
technique. First, this code creates an array f. Then, in the
outer for loop, each element of f is itself initialized as new
Array(); thus f becomes an array of arrays. In the inner for loop,
all elements f[i][ j] in each newly created "inner" array are set to
zero.
var iMax = 20;
var jMax = 10;
var f = new Array();
for (i=0;i<iMax;i++)
{ f[i]=new Array();
for ( j=0;j<jMax;j++)
{
f[i][ j]=0;
}
}
Array Properties

Here is a list of each property and their description.

Property Description Syntax

constructor Returns a reference to the array array.constructor


function that created the object.
index The property represents the zero-based
index of the match in the string
input This property is only present in arrays
created by regular expression matches.
length returns an unsigned, 32-bit integer array.length
that specifies the number of elements
in an array.
prototype The prototype property allows you to object.prototype.name
add properties and methods to any = value
object (Number, Boolean, String and
Date etc).
Array Methods
Here is a list of each method and its description.
Method Description
concat() Returns a new array comprised of this array array.concat(value1,
joined with other array(s) and/or value(s). value2, ..., valueN);
forEach() Calls a function for each element in the array. array.forEach(callback[
callback : Function to test each element of the , thisObject]);
array.
filter() Creates a new array with all of the elements of array.filter(callback[,
this array for which the provided filtering thisObject]);
function returns true. callback : Function to test
each element of the array.
indexOf() Returns the first (least) index of an element array.indexOf(searchEl
within the array equal to the specified value, or ement[, fromIndex]);
-1 if none is found.
join() Joins all elements of an array into a string. array.join(separator);
separator : Specifies a string to separate each
element of the array. If omitted, the array elements
are separated with a comma.
lastIndexOf() Returns the last (greatest) index of an element array.lastIndexOf(sear
within the array equal to the specified value, or chElement[,
-1 if none is found. fromIndex]);
pop() Removes the last element from an array and returns that element. array.pop();

push() Adds one or more elements to the end of an array and returns the new length array.push(element1, ...,
of the array. elementN);
reverse() Reverses the order of the elements of an array -- the first becomes the last, array.reverse();
and the last becomes the first.

shift() Removes the first element from an array and returns that element. array.shift();

slice() Extracts a section of an array and returns a new array. begin : Zero-based array.slice( begin [,end] );
index at which to begin extraction. As a negative index, start indicates an
offset from the end of the sequence.
end : Zero-based index at which to end extraction.
some() Returns true if at least one element in this array satisfies the provided testing array.some(callback[,
function. thisObject]);
sort() Sorts the elements of an array. compareFunction : Specifies a function that array.sort( compareFunctio
defines the sort order. If omitted, the array is sorted lexicographically. n );

splice() Adds and/or removes elements from an array. array.splice(index,


howMany, [element1][, ...,
elementN]);
toString() Returns a string representing the array and its elements. array.toString();

unshift() Adds one or more elements to the front of an array and returns the new length array.unshift( element1, ...,
of the array. elementN );

You might also like