Defining an array in JavaScript


There are 3 ways of defining an array in JavaScript

  1. Conventional arrayThe classic conventional array looks like the following:
    var mystudents=new Array()
            mystudents[0]="Bob"
            mystudents[1]="Jane"
            mystudents[2]="Peter"

    You can expand and contract the array as desired, by adding new array
    elements. Note that like in most other programming languages, the first array
    element should have an index number of 0.

    With a conventional array, you have the option of presetting the array’s
    length when defining it, by passing in a numeric integer into the Array()
    constructor:

    var mystudents=new Array(3)

    In this case 3 array elements will automatically be created, each with a
    value of “undefined.” It also means probing the property
    mystudents.length will return 3.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s