将未定义的“孔”添加到数组中
- <!DOCTYPE html>
- <html>
- <body>
- <h1>JavaScript 数组</h1>
- <p>添加具有高索引的元素可以在数组中创建未定义的“孔”。</p>
- <p id="demo"></p>
- <script>
- var fruits, text, fLen, i;
- fruits = ["Banana", "Orange", "Apple", "Mango"];
- fruits[6] = "Lemon";
- fLen = fruits.length;
- text = "";
- for (i = 0; i < fLen; i++) {
- text += fruits[i] + "<br>";
- }
- document.getElementById("demo").innerHTML = text;
- </script>
- </body>
- </html>