Javascript – Comma as a separator vs. comma as a delimiter

javascript

In this video, the speaker says:

"Some people get confused about how commas work. They think they
should be delimiters rather than separators. Now you can think about
them either way."

I don't understand the difference between a delimiter and a separator (if there is one). The implication in the video is that now (that trailing commas are valid), one can think of them as delimiters, rather than separators…

Btw the language in question is JavaScript. From my understanding, the comma token has two meanings:

  • as an operator (rarely used)
  • as a separator (in argument and parameter lists, array and object literals, etc.)

I'm not sure how a comma delimiter fits into this…

Best Answer

If you consider the comma as a separator, you use a comma between two items of a sequence to separate them, if you consider it as a delimiter, you put it after each item to indicate where an item ends. See the examples below:

Comma as a separator

var myCars = ["Saab", "Volvo", "BMW" ];

Comma as a delimiter

var myCars = ["Saab", "Volvo", "BMW", ];

I think the video says that you can think of commas both as separators and delimiters because both array examples above are valid. On the other hand in Javascript you can only use the comma as a separator in the parameter list of a function, e.g.

foo(a, b, c) // separator, OK

is valid whereas

foo(a, b, c,) // delimiter, NOT OK!

is not valid.

EDIT

As far as I understand, according to the wikipedia page a separator is a special case of a delimiter, namely one that is put between the different text regions whose boundaries need to be marked. In fact, the wikipedia page names comma-separated values as an example use of delimiters. So, in general you can use delimiters in different ways: before, after, on both sides of the portion of text to be marked.

The reason why I interpreted delimiter as "marker that is put after an item" in the Javascript context was motivated by the array literal example, which is valid also for C, C++, and Java (I think I have seen at least one question on stack overflow regarding this topic).

Another example of similar but different use of a character is that of semicolon as a statement delimiter (C, C++, Java, Ada, ...) and as a statement separator (Pascal). Therefore

if (a > 0)
    printf("Positive\n");
else
    printf("Non positive\n");

is correct C code whereas

IF a > 0 THEN
    WriteLn('Positive'); (* Syntax error here! *)
ELSE
    WriteLn('Non positive');

is no correct Pascal code.

Maybe terminator would be a better / less ambiguous term than delimiter? E.g. one could formulate the quote as follows: "Some people get confused about how commas work. They think they should be item terminators rather than item separators. Now (in many cases) you can think about them either way."