Javascript Array Object

In Javascript Array is special type of object, it is used to store multiple values in a single variable.

The elements in a JavaScript array can have different data types, and they can be accessed by indexing. Javascript allows to array of array, elements of an array can also be arrays. In that case, the array is a multi-dimensional array. JavaScript allows associative arrays, too. In this type of arrays, each stored value is associated with a unique key, which can be used as an index. The unique keys are string types.

Syntax

Here is the simple syntax to call properties and methods of Math.

<html>
<head>
<title>Javascript Array Object</title>
<SCRIPT LANGUAGE="JavaScript">
var arr = new Array ();
var arr = new Array ("Orange", "Mango", "Banana");
// or
var arr = ["Orange", "Mango", "Banana"];

// display the elements of the array
for (var i = 0; i < arr.length; i++) {
    document.write (arr[i]);
}

// you can access the elements of the array by zero-based indices
var firstItem = arr[0];     // "Orange"
var thirdItem = arr[2];     // "Mango"
// you can get the length of the array by the length property
var len = arr.length;       // 3

//Creating multidimensional array
var arr1 = new Array ("A", "B", "C");
var arr2 = new Array (1, 2, 3);
var multiArr = new Array (arr1, arr2);
// or
var multiArr = [arr1, arr2];
// you can access the elements of the array by zero-based indices
var firstRow = multiArr[0];     // same as arr1
var secondRowFirstCell = multiArr[1][0]; // 1
</SCRIPT>
</head>
<body>
</body>
</html>

Array Properties

Here is a list of each array property with description.

PropertyDescription
constructorThis will returns a reference to the array function that created the object.
indexThis property represents the zero-based index of the match in the string
inputThis property is only present in arrays created by regular expression matches.
lengthReflects the number of elements in an array.
prototypeThe prototype property allows you to add properties and methods to an object.

Array Methods

Here is a list of each method of array object with description.

MethodDescription
concat()This will returns a new array comprised of this array joined with other array(s) and/or value(s).
every()This will returns true if every element in this array satisfies the provided testing function.
filter()This will creates a new array with all of the elements of this array for which the provided filtering function returns true.
forEach()Calls a function for each element in the array.
indexOf()This will returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
join()This will joins all elements of an array into a string.
lastIndexOf()This will returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
map()This will creates a new array with the results of calling a provided function on every element in this array.
pop()This will removes the last element from an array and returns that element.
push()This will adds one or more elements to the end of an array and returns the new length of the array.
reduce()This will apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
reduceRight()This will apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
reverse()This will reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
shift()This will removes the first element from an array and returns that element.
slice()This will extracts a section of an array and returns a new array.
some()This will returns true if at least one element in this array satisfies the provided testing function.
toSource()This will represents the source code of an object
sort()This will sorts the elements of an array.
splice()This will adds and/or removes elements from an array.
toString()This will returns a string representing the array and its elements.
unshift()This will adds one or more elements to the front of an array and returns the new length of the array.

Array Example

<HTML>
<TITLE>Example of Number Object</TITLE>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

var famname = new Array(5);
famname[0] = "Mango";
famname[1] = "Apple";
famname[2] = "Banana";
famname[3] = "Orange";
famname[4] = "Sapota";

for (i=0; i<famname.length; i++)
{
document.write(famname[i] + "<br/>");
}
document.write("</br>");

var famname = new Array("Jan","Fab","March","April","May","June");
for (i=0; i <famname.length;i++){
document.write(famname[i] + "</br>");
}

document.write("</br>");
var x = famname.length;         
var y = famname.sort();         

for (i=0; i <famname.length;i++){
document.write(famname[i] + "</br>");
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Try Now

See Also

Javascript Built in String Object
Javascript Built in Number Object
Javascript Built in Math Object
Javascript Built in Date Object
Javascript Built in Arrays Object
Javascript Built in Boolean Object
Javascript Built in RegExp Object
Javascript Built in JSON Object


 
SHARE THIS IF YOU LIKE!
submit to reddit
SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.