15

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...

11
  • 2
    Why does this matter?
    – jfriend00
    Dec 30, 2011 at 20:40
  • 9
    @jfriend00 If there is a difference between the terms delimiter and separator, I want to know it... I like to know stuff :) Dec 30, 2011 at 20:44
  • 4
    @jfriend00 Watch the video. If the IE people weren't thinking as narrowly as you, maybe we wouldn't have yet another javascript portability snafu...
    – yannis
    Dec 30, 2011 at 20:57
  • 3
    @ŠimeVidas Good question. BTW the speaker is Douglas Crockford, senior JavaScript architect at Yahoo, known for popularizing JSON and author of JavaScript: The Good Parts. It's a good think you pay attention to detail, especially when watching a Crockford video, as he rarely wastes any breath for stuff that don't matter.
    – yannis
    Dec 30, 2011 at 21:01
  • 1
    @YannisRizos Yea, I know Crockford. He's a great educator. :) Dec 30, 2011 at 21:22

3 Answers 3

17

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."

4
  • Yes, in parameter lists the comma must be used as a separator, whereas in array literals the comma can either be used as a separator or a delimiter. Or in layman's terms: You can put a comma after the last array element, but you don't have to. Dec 30, 2011 at 21:19
  • While your answer does explain the quote from my question quite well, the Wikipedia page for "Delimiter" provides different definitions. According to that article, a delimiter specifies the boundary between regions. So, the "comma as a separator" example from your question above would be called the comma field delimiter, while the "comma as a delimiter" example isn't represented anywhere on that Wiki page. Dec 30, 2011 at 21:37
  • @Šime Vidas - Wikipedia is just a reference like any other reference, it's not the only reference. Dec 30, 2011 at 22:01
  • @CapeCodGunny Yes, I'm just pointing out that there are confronting definitions. I would like to see definitions from other references, too. Dec 30, 2011 at 22:06
7

A separator would be put between two values, separating them.

one,two,three

A delimiter would delimit a field - it would be on both sides.

,one,two,three,

When taken this way, it makes little difference, so long as one can extract the separate fields out.

This is a completely different issue to that of usage of a comma in a programming language such as javascript. It is about delimiting/separating values within a string.

6
  • 1
    Ah yes, this makes sense. Dec 30, 2011 at 20:44
  • I disagree with your delimiter explanation. Based on your explanation it would be ,one,,two,,three, now the delimiter is on both sides. Dec 30, 2011 at 20:58
  • 1
    @CapeCodGunny - You are misinterpreting. The example I gave has a comma before and after each value. The commas not at the very start and end of the string serve as the end delimiter and start delimiter.
    – Oded
    Dec 30, 2011 at 21:00
  • @Oded This Wikipedia article provides different definitions. According to it, your first example would be a field delimiter, while your second example isn't represented in that article at all. Dec 30, 2011 at 21:38
  • 1
    Word of caution! var myNumbers = ['one','two','three', ]; (comma in the end) would give array length as 3 but because of a comma in the front var myNumbers = [,'one','two','three']; gives length as 4.
    – RBT
    Sep 19, 2017 at 5:42
0

"They think they should be delimiters"

In other words, looks like some people think commas should set boundaries or limits. A beginning and an end-

Think in CSV readers. CSV readers allow you to set the "delimiter" and the "separator". For the sake of the answer, lets change comma as delimiter with a single quote. So

# Field A, Field B, Field C
'Value 1','Value 2', 

The single quote (') acts as a delimiter here. It states a beginning and an end. It comprises the beginning and the end of a sequence of elements that must be read as one. Otherwise, the sequence could be readen as different fields, tokenizing the row in more values than fields. This makes sense because Value 1 could be also Value, 1 and we don't want it to be readen has 2 different fields.

On the other hand. the comma here is a separator, which doesn't bring implicit any beginning or end, just sequentiality. New value after this. Given the above example, the trailing comma is just separating Value 2 from Field C's value. An empty value btw.

I get the feeling that this is, somewhat, what Crowford introduce when he says that IE used to read the trailing comma as a separator (sequence). If we extrapolate the CSV example to the old versions of IE, we find the IE translated the trailing comma into a separator, hence allocated one more position in the array. And empty position btw, distorting the true length.

It's a way to say that some people think that, if Microsoft developers had understood the comma as a delimiter, they wouldn't have allowed the trailing comma, hence the extra and empty array position. Or they would, but they would get rid of it and ignore this last position as the newer versions of ES seems to do.

A matter of perception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.