Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

275
Vistas
how does $: in Svelte works

was reading a svelte tutorial, it's mentioned that $: this is regular in JavaScript, but don't understand it.

let count = 0;
$: doubled = count * 2; 

how would you do this in vanilla JavaScript. checking for code that has been change

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I assume you mean this docs - https://svelte.dev/docs#component-format-script-3-$-marks-a-statement-as-reactive

In this case I think they are talking about programming language syntax feature called "label". In case of JS you may read about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

It is needed to look at Svelte source code attentively, but I guess they use this valid JS syntax pattern $: to mark top-level statements and allow them later to parse and pre-process.

So it's not a black JS magic, but a convention only in Svelte "world".

Update:

I think it used somewhere here in source code - https://github.com/sveltejs/svelte/blob/0d017f482016caa51d34918f79dc0b83f0428fd7/src/compiler/compile/Component.ts#L651

If you want to follow the logic, look at usage of "LabeledStatement".

about 4 years ago · Juan Pablo Isaza Denunciar

0

how would you do this in vanilla JavaScript. checking for code that has been change

There are various approaches to this. E.g. dirty checking by comparing previous values with current values and thus determining changes. This approach has some downsides. Among others, data might be duplicated and performance can degrade for larger amounts of data because of all the necessary comparisons.

Another method would be to make dependencies explicit and only update when one of the dependencies changes. Svelte generates the code necessary for that automatically but it can be done manually as well. If you look at generated code you will find that variables get marked as invalidated which triggers updates to the dependent variables.

E.g. in a click handler that increments a count variable you get:

const click_handler = () => $$invalidate(0, count++, count);

For the reactive statement, the code is only run if the count has been changed:

$$self.$$.update = () => {
    if ($$self.$$.dirty & /*count*/ 1) {
        $: doubled = count * 2;
    }
};

This code is optimized for performance, though. It uses array indexes for faster data access and more lean code. For code intended to be written and read by humans, you might want to not do that. Generally, I would not recommend doing this manually either way; it is more verbose and you have to make sure to always specify dependencies correctly for it to work.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The $: is special Svelte syntax and doesn't "work" in vanilla javascript.

The syntax is compatible with vanilla javascript (it's a labeled statement), therefor tooling like javascript parsers and highlighters don't break.

But vanilla javascript doesn't have any reactivity built-in, so it behaves very different.
(It ignores the $:)

Listening for changes in primitive variables can't be done in vanilla.
Lots of workarounds are available.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda