Chapter 15. JavaScript 4: Objects and Arrays Table of Contents


Creating and initialising arrays in a single



Download 0.78 Mb.
View original pdf
Page6/16
Date04.04.2022
Size0.78 Mb.
#58550
1   2   3   4   5   6   7   8   9   ...   16
chp15
15.2.4 Creating and initialising arrays in a single
statement
Another piece of JavaScript that would result in the same array as VERSION 1 above is the following
// VERSION 2 - all in one line var weekDays = new Array "Monday, "Tuesday, "Wednesday, Thursday, "Friday, "Saturday, "Sunday This single statement combines the declaration of anew variable and Array object with assigning the seven weekday Strings. Notice that we have not had to specify the size of the array, since JavaScript knows there are seven Strings and so makes the array have a size of seven elements. The above examples illustrate that arrays can either be created separately (as in VERSION 1), and then have values assigned to elements, or that arrays can be created and provided with initial values all in one statement VERSION 2). When declaring the array, if you know which values the array should hold you would likely choose to create the array and provide the initial values in one statement. Otherwise the two-stage approach of first creating the array, and then later assigning the values, is appropriate.

Download 0.78 Mb.

Share with your friends:
1   2   3   4   5   6   7   8   9   ...   16




The database is protected by copyright ©ininet.org 2024
send message

    Main page