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



Download 0.78 Mb.
View original pdf
Page9/16
Date04.04.2022
Size0.78 Mb.
#58550
1   ...   5   6   7   8   9   10   11   12   ...   16
chp15
Index
Value weekDays[0]
weekDays[1] weekDays[2]

weekDays[3]
weekDays[4] Friday" If this were then followed by the statement weekDays[6] = "Sunday the JavaScript interpreter will identify the need for the weekDays array to now have seven elements, of which the
5th contains the String "Friday" and the 7th contains "Sunday
Index
Value weekDays[0] weekDays[1] weekDays[2] weekDays[3] weekDays[4] "Friday"


6
Index
Value weekDays[5] weekDays[6] "Sunday" Once created, an array has a length property. This stores the number of elements for which JavaScript has made space. Consider the following statements var weekDays = new Array var months = new Array var bits = new Array, 8, 99); The length of each of these arrays is as follows
• weekDays.length = 7
• months.length = 0
• bits.length = 3 One needs to be careful, though, since making the length of an array smaller can result in losing some elements irretrievably. Consider this code and the following output var bits = new Array, 8, 99, 33, 66, 11); document.write(bits); document.write("
array length " + bits.length); bits.length = 4; document.write("
after change
"); document.write(bits); document.write("
array length " + bits.length); The browser output from such code is As can be seen, after the statement bits.length = 4; the last two array elements have been lost.

Download 0.78 Mb.

Share with your friends:
1   ...   5   6   7   8   9   10   11   12   ...   16




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

    Main page