Why use an underscore in for loops?

Example:
for _, tiles in pairs(game.Workspace.Board)
end

Why use an underscore? Is it just a variable name or does it have a use outside of variable naming?

Single Underscores are used for “discarding” values that would normally be on a variable if that makes sense.

3 Likes

By discarding value you mean instead of defining the value, it’s just a nil?

The _ in this loop represents the index that the value in the table is found at. As @Sythivo stated, it is usually used for discarding values that you wouldn’t use.
You may have seen

for i,v in pairs() do

Where i represents index and v represents variable.

1 Like

The variable would still function properly, just that people would use underscores to signify it as a discarded value, it won’t change the value whatsoever.

3 Likes

I’m aware of index, value, etc.

So basically if you aren’t going to use that value, the status quo is to define the value as an underscore to discard it?

Yes, and most of the time in for loops it is not used, however it is still necessary for it to be there.

1 Like

The _ is there to mean that you aren’t really going to use the value, as most people don’t.
If you want to use the value, most of the time you would use i instead.

2 Likes

underscore can still be defined, and still used, it doesn’t mean it won’t go anywhere, people will use underscore if they want to, it’s just that it is commonly used as to show the value is not used.

Not sure who to give the solution to, but Sythivo technically answered in this thread first. Thanks for the help guys! Also thanks @TestAccount563344, I see you in a lot of places. :slight_smile: