Uses between a Semi-Colon and a Comma?

Hi,

What is a difference between a semi-colon ( ; ), and a Comma ( , ) in Syntax?
They appear to do the same thing with some minor differences.

local thing1, thing2 = "hi", "hello" -- works

local t1; t2 = "hu", "io" -- underlined but no error?

local t3; t4 = "il"; "li" -- errors?

local Item = "hi"; print(Item) -- less Ugly?

local Item1 = "hi", print(Item1) --underlined?

local Table = {1,2,3,4,5} -- works
local TableSemi = {1;2;3;4;5} -- also works?
1 Like

( ; ) Is mostly used in other programming languages, like javascript for an example, I’m pretty sure they added this for javascript users to able to use Lua easier. Anyways, its usualy used for line breaks, for example

local player = game.Players.LocalPlayer;
local character = player.Character or player.CharacterAdded:Wait();

Commas ( , ) are used for same line breaks for example

local Table = {'A','B','C'}

In conclusion they are pretty much the same, they are mostly used for line breaks. I hope this answers your question!

EDIT:

This probably doesn’t work because Lua thinks t3 and t4 are on a different line, so it gets confused on where “il” and “'li” goes.

2 Likes

So they are pretty much the same thing?


I’m pretty sure normal Lua has API to allow C code (Or was built on C) if I remember correctly, but Luau isn’t exactly normal Lua if I understand.

Yes, In C they add ( ; ) after every variable

#include <stdio.h>
int main() {
    double a, b, product;
    printf("Enter two numbers: ");
    scanf("%lf %lf", &a, &b);  
 
    // Calculating product
    product = a * b;

    // %.2lf displays number up to 2 decimal point
    printf("Product = %.2lf", product);
    
    return 0;
}

Roblox probably left this feature to be similar with C or Javascript.

1 Like

Huh, well that’s Interesting, Thanks!

Incorrect.
This is a valid syntax for a table literal:

local Table = { 'A',
  'B',

  'C'
}

There’s no semantic difference between commas (,) and semicolons (;) in this instance.

Multiple statements can span in the same line (with semicolon at the end of the chunk or not) so “line breaks” is irrelevant.

I definitely disagree. I believe it is designed to resolve ambiguities between a new statement and a function call like this:

x = y
(z)()
1 Like

Stylistically, the use of semi-colons are more common in Java, JavaScript too, with the former having importance for the compiler. Preferably in Lua(and Luau), you would use commas over the semi-colons as it is more declared by the style guidelines. The latter should only be used in edge cases where the commas do not apply.

1 Like

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