将未定义的“孔”添加到数组中

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript 数组</h1>
  6.  
  7. <p>添加具有高索引的元素可以在数组中创建未定义的“孔”。</p>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. var fruits, text, fLen, i;
  13. fruits = ["Banana", "Orange", "Apple", "Mango"];
  14. fruits[6] = "Lemon";
  15.  
  16. fLen = fruits.length;
  17. text = "";
  18. for (i = 0; i < fLen; i++) {
  19. text += fruits[i] + "<br>";
  20. }
  21. document.getElementById("demo").innerHTML = text;
  22. </script>
  23.  
  24. </body>
  25. </html>