8.7 ARRAYS IN JAVASCRIPT

The Array object lets you store multiple values in a single variable. It stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Syntax-
Use the following syntax to create an Array Object. As-
var fruits = new Array( “apple”, “orange”, “mango” );
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.
You can create array by simply assigning values as follows:
var fruits = [“apple”, “orange”, “mango” ];
You will use ordinal numbers to access and to set values inside an array as follows.
fruits[0] is the 1st element
fruits[1] is the 2nd element
fruits[2] is the 3rd element

Properties of array-
(i) Length
(ii) Prototype
(iii) Methods (readymade functions) of array

Length-
Array length property returns an unsigned, 32-bit integer that specifies the number of elements in an array.
Syntax-
array.length
Return Value
Returns the length of an array.
Example:
<html>
<head>
<title>JavaScript Array length Property</title>
</head>
<body>
<script type= “text/JavaScript”>
  var arr = new Array( 10, 20, 30 );
  document.write(“arr.length is:” + arr.length);
</script>
</body>
</html>
Output-
arr.length is: 3

Prototype-
The property of prototype permits you to add properties and methods to any object viz. (Number, Boolean, String, Date etc).
Syntax-
object.prototype.name = value
Example:
<html>
<head>
<title>User-defined objects</title>
<script type= “text/JavaScript”>
function book(title, author)
{
this.title = title;
this.author = author;
}
</script>
</head>
<body>
<script type= “text/JavaScript”>
  var myBook = new book(“Programming in C”, “Kanitkar”);
book.prototype.price = null;
myBook.price = 100;
document.write(“Book title is : ”  + myBook.title + “<br>”);
document.write(“Book author is : ”+ myBook.author + “<br>”);
document.write(“Book price is : ” + myBook.price + “<br>”);
</script>
</body>
</html>
Output:
Book title is : Programming in C
Book author is : Kanitkar
Book price is : 100
Methods (readymade functions) of array-

2

2

Example: 
Here is an example of array method concat().
JavaScript array concat() method returns a new array comprised of this array joined with two or more arrays.
Syntax-
array.concat(value1, value2, ..., valueN);

Parameter Details
valueN : Arrays and/or values to concatenate to the resulting array.
Return Value
Concatenated values of different arrays in the resulting array
Example:
<html>
<head>
<title>JavaScript Array concat Method</title>
</head>
<body>
<script type="text/JavaScript">
var alpha = [“a”, “b”, “c”];
var numeric = [1, 2, 3];
var alphaNumeric = alpha.concat(numeric);
document.write(“alphaNumeric : ” + alphaNumeric );
</script>
</body>
</html>
Output:
alphaNumeric: a,b,c,1,2,3

Example:
array pop() method which returns top(last in) element.
<html>
<body>
<h2>JavaScript Array Methods</h2>
<h2>pop()</h2>
<p>The pop() method removes the last element from an array.</p>
<p id= “prev”></p>
<p id= “last”></p>
<script>
var fruits = [“banana”, “apple”, “orange”, “mango”];
document.getElementById(“prev”).innerHTML = fruits;
fruits.pop();
document.getElementById(“last”).innerHTML = fruits;
</script>
</body>
</html>

Example: 
array pop() method which returns top(last in) element.
<html>
<body>
<h2>JavaScript Array Methods</h2>
<h2>pop()</h2>
<p>The pop() method removes the last element from an array.</p>
<p id= “prev”></p>
<p id= “last”></p>
<script>
var fruits = [“banana”, “apple”, “orange”, “mango”];
document.getElementById(“prev”).innerHTML = fruits;
fruits.pop();
document.getElementById(“last”).innerHTML = fruits;
</script>
</body>
</html>

Licensed under the Creative Commons Attribution Share Alike License 4.0

Made with eXeLearning (New Window)