Earlier, we have learnt how to create an array. Over here, we will learn how to print the items in an array using a for loop.
Using the same example in array:
const colours = ['red','orange','yellow','green','blue','indigo','purple'];
Now, let's try printing the item in the array using a for
loop as shown below:
for (let count = 0; count < colours.length; count ++){
console.log(colours[count]);
}
ℹ️
colours[count]
refers to the item in arraycolours
, with the index number equivalent to count. Ifcount = 0
,colours[count]
-->colours[0]
-->'red'