The difference between **for i,v in table do** and **for i,v in pairs(table) do**

The difference between for i,v in table do and for i,v in pairs(table) do I saw inside the knit. i couldn’t see the difference

1 Like

Are you talking about:

for i, v in next, table do

end

Possibly?

for i,v in table do

– is generalized iteration typical for luau. It’s extended semantics you won’t find in vanilla lua.

So in normal lua you have to use next, or functions like ipairs and pairs which return an interator. ipairs() keeps the order as the i stands for ‘index’ and it respects the order in a sequential array.

It visits every element, but keeps consecutive order for sequential arrays, just like ipairs does. And with keys, order is unspecified just like when using pairs.

1 Like

The first one is generalized iteration which afaik tends to be faster than any of the other iteration approach (ipairs, pairs and next). Here’s the RFC that proposed it; you can go over the motivation part for why it was implemented.