This is just a quick question, but I was wondering what the difference between using brackets and quotes and using neither is. Both of them work correctly, but I would like to know if there is a reason to use one over the other.
Example:
There isn’t really a difference between the two except you can make more complicated names for indexes with brackets. You can read most this on the lua reference manual.
Thanks. I wasn’t sure what to search so I just posted it here. If I’m getting this right, using [“Name”] just allows spaces and more characters.
Additionally, you can put any value between the brackets, not just strings. You can use values like booleans, Instances, and you can also use expressions. For example, if you’re making an array-like table but you want to make the indices clearer, you could do it this way:
local t = {
[1] = "first",
[2] = "second",
[3] = "third",
--skip 4. if you index it, it will evaluate to nil
[5] = "fifth",
[3 * 2] = "sixth" --expression used
}
Square braces are required for indices that are not string literals.
local t = {
k1 = 123, --These keys are implicitly string values.
k2 = "abc"
}
local t = {
["k1"] = 123, --These keys are explicitly string values.
["k2"] = "abc"
}
local t = {
["space key"] = 123 --Square braces are required for string keys that contain a whitespace.
}