Array Properties

Useful properties of arrays.

Examples

Array Length

Finding the number of items in an array.

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let len = fruits.length;
console.log(len); // 4

Access Last Element

Using length to access the last item.

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let last = fruits[fruits.length - 1];
console.log(last); // Mango

Looping with Length

Iterating through an array using a for loop and length.

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = "<ul>";
for (let i = 0; i < fruits.length; i++) {
  text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";
document.getElementById("demo").innerHTML = text;

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.