Is there a difference between these two?

I always think I’m forgetting the basic stuff.

What’s the difference between this:

local newtable = {}
newtable.Entry1 = "hello"

and this:

local newtable = {}
newtable[Entry1] = "hello"

The first one, dot syntax, treats the key as a string. The second one, bracket syntax, treats the key as a variable.

The equivalent of the first example in bracket syntax would be newtable["Entry1"], not newtable[Entry1]. The second example is just trying to index newtable with the value of the variable Entry1, which doesn’t exist, so it’s equivalent to indexxing it with nil.

2 Likes

Oh, so the first one’s entry is

“Entry1” – string

and the 2nd one’s entry is Entry1 as a variable, correct?

1 Like

Yeah, that’s right. The second example is more flexible, since you can put any type in. For example, you could index the table with a number like newtable[1], which is a very common usecase.

3 Likes

Thanks for your help! You always seem to be around when I need something small to be quickly answered lol

1 Like