Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Svelte Writable Stores n+1 vs n++

Working through the official svelte example of writable-stores https://learn.svelte.dev/tutorial/writable-stores

Wondering why this works in updating count

    function increment() {
        count.update((n) => n+1);
    }

and this does not?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    function increment() {
        count.update((n) => n++);
    }

>Solution :

n++ is a post-increment operator. It returns the current value, then increments the variable. In this case, you want to use the pre-increment operator, which looks like ++n. It first increments the value, then returns the already incremented value.

function increment() {
    count.update((n) => ++n);
}

Javascript increment operator

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading