Getting Started with Svelte: A Beginner’s Guide

Getting Started with Svelte: A Beginner’s Guide


Svelte is a modern JavaScript framework for building user interfaces. Unlike other frameworks such as React or Vue, Svelte compiles your code to efficient, imperative code that directly manipulates the DOM. This results in faster initial load times and smoother updates. In this guide, we’ll walk you through the basics of getting started with Svelte.

Setting Up Your Environment

Before you can start coding, you’ll need to set up your development environment. You’ll need Node.js and npm installed on your computer. Once you have these, you can install the Svelte template by running the following command in your terminal:

npx degit sveltejs/template svelte-app

This will create a new directory called svelte-app with a basic Svelte project structure.

Understanding the Basics

A Svelte application is composed of components. Each component is a reusable, self-contained piece of code that encapsulates HTML, CSS, and JavaScript. Here’s what a basic Svelte component looks like:

  let name = 'world';

<h1>Hello {name}!</h1>

In the script tag, we declare a variable name. In the HTML, we use curly braces {} to display the value of name.

Building Your First Svelte App

Now that you understand the basics, let’s build a simple Svelte app. We’ll create a counter that increments when you click a button. Replace the contents of App.svelte with the following code:

  let count = 0;

  function handleClick() {
    count += 1;

<button on:click={handleClick}>
  Clicked {count} {count === 1 ? 'time' : 'times'}

Here, we’ve added a handleClick function that increments count each time the button is clicked. The on:click attribute in the button tag is an event listener that calls handleClick when the button is clicked.


Congratulations! You’ve just built your first Svelte app. While this guide only scratches the surface of what’s possible with Svelte, it should give you a good foundation to start exploring on your own. Happy coding!

Remember, the best way to learn is by doing. So, don’t hesitate to get your hands dirty and start building with Svelte. You’ll be amazed at how intuitive and powerful it is. Happy Svelte-ing!