Question about keys with quotation marks in dictionaries

I looked at the dictionaries intro in the docs and saw that you can format keys using quotations marks like ["this"], but now I wondered what’s the difference when using it with quotations and without them, like this

For example as to what I’m asking:

local dictionary = {
	Value1 = "No quotations",
	["Value2"] = "With quotations"
}

for i, v in pairs(dictionary) do
	print(i, v)
end

Wouldn’t this print the keys as the exact same?

There is no difference, they behave the same way.

Using no quotation marks must follow standard variable syntax in Lua. This means you can’t have spaces, etc. Using quotation marks and square brackets allows you to use more characters, such as spaces.

local dict = {
    hi there = 3
}
--> expected "}" to close "dict", got "there"

local dict = {
    ["hi there"] = 3
} --> no error

EDIT: ok it seems that the person below me beat me to it while i was double checking my sources but ill leave this here anyway…

Yes, but the difference is that without the square brackets and quotes, you cannot use any special characters besides an underscore _, including spaces, and cannot start the key with a number, same way how you would index something like this:

local myHouse = workspace["1. My Special House!"]

You also need the square brackets to set a key other than a string:

local part = Instance.new("Part")

local t = {
    part = true -- will just be a string key 'part'
    [part] = true -- will be the Instance key of part
}

Edit: In terms of readability, I would recommend using the square brackets and quotation as they immediately contrast with the default white text:
image

2 Likes

This looks ugly to me so I try to avoid using it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.