WHAT'S NEW?
Loading...

Array Properties - Constructor, index, Prototype, input, length in javascript with example - Tech-n-Savvy Blogger



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.

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 first element

fruits[1] is the second element

fruits[2] is the third element


Array Properties

Here is a list of the properties of the Array object along with their description.

Property
Description







constructor
Returns a reference to the array 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
Reflects the number of elements in an array.




prototype
The prototype property allows you to add properties


and methods to an object.





In the following sections, we will have a few examples to illustrate the usage of Array properties.

constructor

Javascript array constructor property returns a reference to the array function that created the instance's prototype.

Syntax

Its syntax is as follows:

array.constructor

Return Value

Returns the function that created this object's instance.

Example

Try the following example.

<html>

<head>

<title>JavaScript Array constructor Property</title>

</head>

<body>

<script type="text/javascript">

var arr = new Array( 10, 20, 30 ); document.write("arr.constructor is:" + arr.constructor);

</script>

</body>

</html>



Output

arr.constructor is:function Array() { [native code] }


length

Javascript array length property returns an unsigned, 32-bit integer that specifies the number of elements in an array.

Syntax

Its syntax is as follows:

array.length

Return Value

Returns the length of an array.

Example

Try the following 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 prototype property allows you to add properties and methods to any object (Number, Boolean, String, Date, etc.).

Note: Prototype is a global property which is available with almost all the objects.

Syntax

Its syntax is as follows:

object.prototype.name = value

Example

Try the following 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("Perl", "Mohtashim"); 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 : Perl

Book author is : Mohtashim

Book price is : 100


0 comments:

Post a Comment