I came across the following valid syntax in JS when looking at svelte
library:
$: doubled = 6 * 2;
At first, I thought it was specific for the library, but it works on the Chrome console. What is this syntax?
It can be anything:
name: something = 6 * 2;
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;
}
}
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.