What is this : sign after a variable JS syntax?

Any JavaScript statement (kind-of except function declarations) can be preceded by a label:

foo: var x = 0;

What you’ve got there is something like that:

$: doubled = 6 * 2;

In your statement, “$” is the label.

There’s not much point to labelled statements because there’s no goto in JavaScript. Both break and continue can include a label of an enclosing loop to indicate how many “layers” should be involved.

wholeLoop:
for (let i = 0; i < matrix.length; i++) {
  for (let j = 0; j < matrix[i].length; j++) {
    if (matrix[i][j] == null)
      // Oh no! This is terrible
      break wholeLoop;
  }
}

MDN, spec


All the above is pretty much correct, but apparently Svelte applies its own build-time preprocessor to component source code and translates that into the actual JavaScript sent to the browser. This use of the label syntax is “hijacked” by them to mean something; see Quentin’s answer.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)