Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!