This section provides a quick description of what is an array in JavaScript and a tutorial example showing JavaScript array features.
Like many programming languages, JavaScript support arrays allowing you to store and retrieve a list of elements.
But like some other languages, JavaScript does not have an explicit array data type.
Instead, it provides you an Array object type to let you create arrays as objects.
Here are the main features of an array in JavaScript:
An array is not a primitive data type.
An array is an object.
Array elements are indexed. Elements can be stored and retrieved by indexes.
Array's index starts from 0. In other words, index 0 represents the first element.
The size of an array is not statically defined. It can be dynamically expanded or truncated.
Array's elements do not have to be the same data type.
Several methods are provided on array objects allowing you to manipulate arrays directly.
Here is a tutorial example JavaScript that shows you some of the features mentioned above:
<html>
<!-- Array_Features.html
Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
-->
<head><title>Array Features</title></head>
<body>
<pre>
<script type="text/javascript">
// Creating a new array of mixed data types
var account = ["Saving", 999.99, true];
// Store new value to the array
account[2] = false;
// Retrieving an element
document.write("Account type: "+account[0]+"\n");
// Calling an array method
account.push(5.25/100);
document.write("Interest rate: "+account[3]*100+"%\n");
</script>
</pre>
</body>
</html>