Vì vậy, những gì các dòng trước đó đang làm là tạo ra một mảng mới bên trong phần tử có chỉ số 0 của mảng nhân viên của chúng tôi. Trong ba dòng tiếp theo, bạn đặt giá trị vào các nhân viên mới được tạo ra [0] array. JavaScript làm cho nó dễ dàng để làm điều này: Bạn chỉ cần nhà nước tên của mảng, x | Chapter 4 JavaScript An Object-Based Language Note that in your for statement you ve used the Array object s length property in the condition statement rather than inserting the length of the array 5 like this for elementindex 0 elementindex 5 elementIndex Why do this After all you know in advance that there are five elements in the array. Well what would happen if you altered the number of elements in our array by adding two more names var names new Array Paul Sarah Louise Adam Bob Karen Steve If you had inserted 5 rather than your loop code wouldn t work as you want it to. It wouldn t display the last two elements unless you changed the condition part of the for loop to 7. By using the length property you ve made life easier because now there is no need to change code elsewhere if you add array elements. Okay you ve put things in ascending order but what if you wanted descending order That is where the reversed method comes in. The reverse Method Putting Your Array into Reverse Order The final method you ll look at for the Array object is the reversed method which no prizes for guessing reverses the order of the array so that the elements at the back are moved to the front. Let s take the shopping list again as an example. Index 0 1 2 II3 II4 Value Eggs Milk Potatoes Cereal Banana If you use the reversed method var myShopping new Array Eggs Milk Potatoes Cereal Banana you end up with the array elements in this order Index 0 1 2 3 4 Value Banana Cereal Potatoes Milk Eggs To prove this you could write it to the page with the join method you saw earlier. var myShoppingList br myShoppingList 135 Chapter 4 JavaScript An Object-Based Language Try It Out Sorting an Array When used in conjunction with the sort method the reverse method can be used to sort an array so that its elements appear in reverse alphabetical or numerical order. This is shown in the following example html body script language JavaScript