__ a special character?

In many guides and tutorials I see that some variables are named with _ before the actual name or even _ alone is used as a name like in

for _,k in pairs

Is it just a convenction or this _ symbol has a special meaning in lua? Also recently I noticed even double floor like in __index. Why?

In this case, _ is used to express the intent that this variable won’t be used. It’s simply a placeholder name.

This tends to happen when we don’t use the index / key when doing an iteration.

This one is a bit different. Metamethods start with two underscore because they are special. By doing so, developers can avoid potential name collisions, and we don’t restrict the use of keys like index or _index.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.