Why use pairs and ipars instead of just indexing through the array with i?

Say I wanted to index through an array. This is how I would usually do it:

for i = 1, #array do
    array[i]
end

And this is how you’d do it with pairs:

for i, v in pairs(array) do
    v
end

What is the difference here? Is it actually just two ways to do the same thing?

you can’t use the first method with a dictionary, pairs looks better anyways

The first method only works with tables that aren’t dictionaries. What do I mean with this? A dictionary is a table that has non-numeric keys. For example, local dict = {a = 1, b = 2}. Since you can’t use the length property on a dictionary, you should use the second method instead.

So the reason why D0RYU said that it looks better is because it’s just a better habit to get into?

Correct, you should try to use the second one all the time to make a habit.

To answer the “why” for ipairs as well: it’s because it ensures indexes are sorted. Using pairs does not guarantee order.