Difference between ["Name"] and Name in tables

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:

module.PlateTypes = {
	Fire = {
		Damage = "15"
	}

as opposed to

module.PlateTypes = {
	["Fire"] = {
		Damage = "15"
	}
1 Like

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.

6 Likes

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.

4 Likes

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
}
3 Likes

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.
}

you can also use a variable, instance, or really anything

t = {
   [game.Players.LocalPlayer] = 5
}
2 Likes

That’s not a variable, that’s an instance you got by indexing, which I would consider an expression.

The point is true though, we can use a variable. I think it could be considered an expression but thanks for explicitly pointing it out.

1 Like

my brain went somewhere else with the example lol

2 Likes