1. Home
  2. /
  3. Svelte
  4. /
  5. Counter with LocalStorage

Counter with LocalStorage - Svelte Typing CST Test

Loading…

Counter with LocalStorage — Svelte Code

Persists counter value using localStorage in Svelte.

<script>
	let count = parseInt(localStorage.getItem('count') || '0', 10);
	function increment() { count += 1; localStorage.setItem('count', count); }
	function decrement() { count -= 1; localStorage.setItem('count', count); }
	function reset() { count = 0; localStorage.setItem('count', count); }
</script>

<h2>Counter: {count}</h2>
<button on:click={increment}>+</button>
<button on:click={decrement}>-</button>
<button on:click={reset}>Reset</button>

Svelte Language Guide

Svelte is a modern, component-based JavaScript framework that shifts work from the browser to the build step. Instead of using a virtual DOM, Svelte compiles components into highly optimized vanilla JavaScript, delivering faster performance and smaller bundles.

Primary Use Cases

  • ▸Single-page applications (SPAs)
  • ▸Highly interactive UI widgets
  • ▸Performance-critical front-end apps
  • ▸Progressive web apps (PWAs)
  • ▸Embeddable components for websites

Notable Features

  • ▸Compile-time reactivity (no virtual DOM)
  • ▸Component-based architecture
  • ▸SvelteKit for routing, SSR, and full-stack apps
  • ▸Built-in stores for state management
  • ▸CSS scoped by default

Origin & Creator

Created in 2016 by Rich Harris, a graphics editor at The Guardian, aiming to simplify reactive UI development.

Industrial Note

Svelte is highly suited for performance-sensitive web apps, small-to-medium SPAs, interactive widgets, and animation-heavy projects.

Quick Explain

  • ▸Svelte allows developers to build reactive and component-driven interfaces with minimal boilerplate.
  • ▸It compiles code at build time, eliminating the need for a virtual DOM.
  • ▸Svelte prioritizes performance, simplicity, and a clean development experience.

Core Features

  • ▸Reactive assignments (`count += 1` updates UI)
  • ▸Svelte stores (`writable`, `readable`, `derived`)
  • ▸Component scoping with `<script>`, `<style>`, `<template>` style syntax
  • ▸Lifecycle hooks (`onMount`, `beforeUpdate`, `afterUpdate`, `onDestroy`)
  • ▸Animations, transitions, and motion primitives

Learning Path

  • ▸Learn Svelte reactivity
  • ▸Understand component structure
  • ▸Work with stores
  • ▸Learn SvelteKit routing & endpoints
  • ▸Build full-stack SvelteKit apps

Practical Examples

  • ▸Counter app with reactive variables
  • ▸Animated card slider
  • ▸Interactive charts dashboard
  • ▸SvelteKit blog with SSR
  • ▸PWA with offline caching

Comparisons

  • ▸Faster than React/Vue due to compile-time model
  • ▸Less boilerplate than Angular
  • ▸Simpler reactivity vs Vue’s computed/watchers
  • ▸SvelteKit provides full-stack tools like Next.js
  • ▸Great for performance-focused applications

Strengths

  • ▸Extremely lightweight and fast
  • ▸Minimal code and easy syntax
  • ▸Built-in transitions and animations
  • ▸No virtual DOM = fewer performance bottlenecks
  • ▸SvelteKit is robust for full-stack development

Limitations

  • ▸Smaller ecosystem compared to React/Vue
  • ▸Less enterprise adoption
  • ▸Fewer third-party libraries
  • ▸SSR and advanced concepts tied heavily to SvelteKit
  • ▸Learning new patterns unique to Svelte’s compilation model

When NOT to Use

  • ▸Large enterprises requiring long-term ecosystem guarantees
  • ▸Apps relying on niche third-party React/Vue libraries
  • ▸Projects requiring established patterns like Redux
  • ▸Heavy-duty CMS integrations without SvelteKit adapters
  • ▸Teams unfamiliar with Svelte’s reactivity model

Cheat Sheet

  • ▸{#if} - conditional blocks
  • ▸{#each} - list loops
  • ▸$store - subscribe to store
  • ▸export let x - props
  • ▸bind:value - two-way binding

FAQ

  • ▸Does Svelte use a virtual DOM?
  • ▸No, Svelte updates the DOM directly via compiled code.
  • ▸Is Svelte reactive?
  • ▸Yes, reactivity is built into variable assignments.
  • ▸Is Svelte beginner friendly?
  • ▸Yes, it has one of the simplest UIs and reactivity models.
  • ▸Does Svelte support TypeScript?
  • ▸Yes, Svelte and SvelteKit fully support TypeScript.
  • ▸Is Svelte good for production apps?
  • ▸Yes, especially with SvelteKit for routing and SSR.

30-Day Skill Plan

  • ▸Week 1: Svelte basics & reactivity
  • ▸Week 2: Components, props, stores
  • ▸Week 3: Transitions, animations, advanced reactivity
  • ▸Week 4: SvelteKit routing & server endpoints
  • ▸Week 5: Deployments & performance tuning

Final Summary

  • ▸Svelte is a fast, compile-time framework with minimal boilerplate.
  • ▸Its reactivity model is extremely intuitive.
  • ▸Ideal for small to mid-size apps, animations, and PWAs.
  • ▸SvelteKit provides an opinionated full-stack solution.
  • ▸A top choice for performance-sensitive front-end apps.

Project Structure

  • ▸src/ - main source code
  • ▸src/routes/ - SvelteKit routing
  • ▸src/lib/ - components and utilities
  • ▸static/ - static assets
  • ▸svelte.config.js - framework config

Monetization

  • ▸Freelance SvelteKit projects
  • ▸Commercial dashboards
  • ▸Animated web experiences
  • ▸Component libraries
  • ▸SaaS apps with full-stack SvelteKit

Productivity Tips

  • ▸Use reactive declarations for cleaner logic
  • ▸Split stores into modular files
  • ▸Use transitions for delightful UX
  • ▸Leverage SvelteKit load functions
  • ▸Reuse patterns via slots and components

Basic Concepts

  • ▸Reactive variables
  • ▸Props passing
  • ▸Bindings (`bind:value`)
  • ▸Logic blocks (`{#if}`, `{#each}`, `{#await}`)
  • ▸Stores and subscriptions

Official Docs

  • ▸https://svelte.dev/docs
  • ▸https://kit.svelte.dev/docs
  • ▸https://svelte.dev/tutorial

More Svelte Typing Exercises

Svelte Counter ComponentSvelte Counter with Reactive StatementSvelte Counter with Theme Toggle and Computed ClassSvelte Counter with Derived StateSvelte Counter with Event DispatchSvelte Counter with Slot ControlsSvelte Counter with Reactive ThemeSvelte Counter with Lifecycle HookSvelte Counter with Two-way Binding

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher