Difference between square brackets and not in dictionary

What’s the difference between using square brackets over a name and not for items in a dictionary in a dictionary?

local dictionary = {
["Hi"] = "This"
}
local dictionary = {
"Hi" = "This"
}

Square brackets in tables are effective for indexes with whitespaces like Hi there. You can avoid using square brackets by writing the index as a variable inside instead.

You can’t do the second method though, that’s not defining the index of the table.

1 Like

You could still do

"hi there" = "hi"

or could you not?

You can’t do that, because Lua mistakes it as setting the first string into the table and the = is unexpected.

1 Like

What’s the index of the table?

An index is what key it is. I actually meant key, where it is just something you index to acquire its value from a table.

1 Like

So I’d get the value of hi there like this?

local dictionary = {
["Hi there"] = "value"
}
dictionary["Hi there"]

That’s correct. dictionary["Hi there"] == "value" is true.

1 Like

Going forwards on Operatik’s response:

dictionary.Hi is the same as dictionary["Hi"] > Syntax sugar.

1 Like