What does ; do?

I’m really curious on what ; does in scripting, I’ve seen it being used various times on variables and things like end like after it for example:

Variable;
end;

I know this is kind of a simple question, But I’ve genuinely tried to search it various times, however I couldn’t find any answers in the search results.
Anyways thank you for reading, if you’re read this far :camel:

6 Likes

It doesn’t do anything really, it’s just preference. People who know JS typically use that as they’re used to semicolons. Personally, I just think it’s a little longer to do so, but it’s really your choice. Lua doesn’t error when using them, it behaves normally.

7 Likes

They separate multiple statements on the same line. See @posatta’s example below :arrow_down:

2 Likes

That’s not entirely true, there are a few niche situations where it’s necessary to clarify ambiguity. It’s used clarify that two expressions are completely separate. I gave an example in this post:

5 Likes

In other programming languages like Java, C++, C#, etc. the semicolon is a statement separator. Some just like bringing over their habits from those languages. In Lua they really only have 2 uses.

Some people often use semicolons rather than commas (,) to delimit table entries:

local t = {
    a = 0;
    b = 1;
    c = 2
}

Which is valid.

They are also used to prevent ambiguous syntax:

local n = 7/(3 + 0.5)

(function()
    print("hi")
end)()

In this example it can be interpreted in 2 ways: local n = 7/(3 + 0.5) can either be its own statement, or due to the parentheses at the bottom, it could be seen as continuing off the first statement.

7/(3 + 0.5)(function() print("hi") end)()

It could be seen like you’re trying to call the number.

Or it could be seen as

local n = 7/(3 + 0.5); -- notice the ;

(function()
    print("hi")
end)()

That is where a semicolon would be useful.

12 Likes

Just gonna drop this here (from wikipedia):

An influential and frequently-cited study in this debate was Gannon & Horning (1975), which concluded strongly in favor of semicolon as a terminator: “The most important [result] was that having a semicolon as a statement terminator was better than having a semicolon as a statement separator.”

1 Like