What is for _,?

I’ve looked at youtube, searched the dev forum and dev hub, and still I cannot find anything about this. What is _, ? I tested around with it but it gave me many errors and I just stopped.

17 Likes

I may just be stupid, but what _ are you talking about?

The:

for _, v in pairs() do
        --Code
end

If for that its simply another character for ‘i’. you can put:
Or

_G.
1 Like

its used for for loops if you have no use for the index then thats where you use _

1 Like

Does that help with performance at all? (just asking)

2 Likes

yes it does

30char

3 Likes

It’s just another character for i:


for i, v in pairs() do

end

and

for _, v in pairs() do

end


are both the same, even the v can be replaced as another character. The reason why we use “_” is because we aren’t going to be using it, either way its not a big deal.

13 Likes

It’s a placeholder variable that replaces a variable you won’t use. This is commonly used in pairs and ipairs iterator loops but they’re used in more than just that.

For example, they may replace parameters.

local function example(_, param2)

Or replace specific variables in a group of variables (tuple):

local _, pos = workspace:FindPartOnRay(exampleRay)

They cannot be used in some cases like tables and arguments since nil would take their place, basically any variable you’d be expecting yet don’t need, can be replaced with _.

15 Likes

Do you mind explaining how replacing one character with an another one could result in help with performance?

6 Likes

Can you confirm that this does actually help with performance thought?

Like test the code’s speed in both cases or something because I haven’t seen any confirmation about if it actually helps with performance, prove that to us.

2 Likes

You could check the performance by probably doing:

for _, v in pairs(workspace:GetChildren()) do
          v:Destroy()
          print(Descendants are destroyed)
end

and do the same with the i but in another script and see which one prints out faster. I’m not too sure if it will speed up performance though, I’m just in the middle.

1 Like

No, it is a normal variable and you can still interact with it.

No, it doesn’t

3 Likes

It doesn’t affect performance at all.

Using the underscore _ for a variable (including when it’s just prefixed, such as _something, disregarding private fields in a class), is used to indicate an unused variable. That’s it. Nothing more.

It’s useful as a programmer to tell you “oh I don’t need to use that”. If you did name it, you might think you used it, which could become confusing. Modern linters will utilize this too.

22 Likes

i was going to describe why the person above was wrong with all of your points but ill instead focus on showing proof that this is true, because its the only thing i can give now

lua 5.1.5 test

4 Likes