JavaScript. Working with arrays

The Array object is used to store multiple values in a single variable.

An array is a special variable, which can hold more than one value at a time. Most tutorials start out introducing you to arrays as such var myArray = new Array(10);

Current best-practice eschews the "new" keyword on Javascript primitives. If you want to create a new Array simply use brackets [] like this var myArray = [];

The Array object let's you store multiple values in a single variable.

Syntax:

Creating a 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 each property and their 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.


Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: