Svelte for Beginners_ A Guide
Svelte for Beginners_ A Guide
Author
Nimrod Kramer
@NimrodKramer
Table of contents
Key Features
Benefits
Svelte's Compiler
Setting Up a Svelte Development E
Prerequisites
Project Setup
Understanding Svelte Components
<script>
Markup
frameworks that rely on a virtual DOM, Svelte compiles your code to Project Setup
Components
mized vanilla JavaScript at build time. This guide introduces you to the
Features
Introducing SvelteKit
velte, demonstrating how it differs from other frameworks, and walks
Core Features
h setting up your development environment, creating components, Best Practices
Folder Structure
ging state and events. You'll also get a sneak peek into SvelteKit for
Naming
nced projects and best practices for working with Svelte. Whether Formatting
ginner or looking to expand your web development toolkit, this guide Components
State
hing for you. Testing
Build
Start reading - Free forever
hat Svelte is and how it differs from other frameworks. Where To Go From Here
Learn Advanced Svelte
our Svelte development environment. Join the Community
Put Your Skills to Use
and the structure and syntax of Svelte components. Conclusion
Related Questions
state and events within your app.
Is Svelte good for beginners?
What is the easiest way to get star
advanced concepts and SvelteKit for building complex applications.
Does Svelte have a future?
est practices for coding and organizing your Svelte projects. What are the cons of Svelte?
Related Blog Posts
tures
ome cool things about Svelte:
Why not level up your
ty - Svelte automatically updates parts of your app when the data changes, so with daily.dev?
ons - Svelte makes it easy to add smooth animations when things in your app
or move around.
bility - Svelte helps make your app more accessible to people with disabilities by
s
ight like using Svelte:
ance - Apps made with Svelte run really well because there's no extra baggage
ng a framework.
size - Svelte apps can be smaller than others because they don't include
sary code.
er experience - Svelte makes coding more pleasant with features like error
Compiler
s a special tool called a compiler to turn your .svelte files into
aScript. Here's what makes it special:
of-time compilation - Svelte gets your code ready before your app even starts,
neration - The code Svelte generates is clean and simple, doing exactly what's
aking - Svelte only includes the parts you use in your app, so there's no wasted
ecking - Using TypeScript, Svelte checks your code for mistakes, making it more
- Since Svelte is all about building apps with JavaScript, you need Node.js
12.22 or newer) to run JavaScript on your computer, not just in a web browser.
pnpm - These are tools that let you add Svelte and other useful stuff to your
npm comes with Node.js, but you can get pnpm by itself if you prefer.
DE - Any text editor works for writing Svelte code, but using one like VS Code
Setup
rted with a Svelte project is straightforward. Here's a simple way using
svelte@latest my-svelte-app
use a tool called degit to copy the official Svelte starter template:
sveltejs/template my-svelte-app
lte-app
ll
npm install to add any needed libraries, and npm run dev to start a
nt server. Now, as you change your Svelte files, your app will update
he changes in your web browser automatically.
pt> part is where you write JavaScript code for the component. Here,
things like:
other components
ce:
ing up a variable
nt = 0;
ng a prop
let name;
mple function
n handleClick() {
+= 1;
p part is basically your HTML. It's where you decide how your
t looks and behaves. Svelte adds some cool features to HTML like:
ith #each
licks yet</p>
-->
ems as item}
em.name}</li>
eholder -->
lot>
e> part is for CSS, which styles your component. These styles only
component, preventing any mix-ups with the rest of your app.
{
ese styles are just for this button */
tate
ck of information inside a component, just use let :
= 0;
s a variable count that will automatically update in your component
t changes.
= 0;
handleClick() {
= 1;
n:click={handleClick}>
{count} {count === 1 ? 'time' : 'times'}
g Events
omething happen when a user does something, like clicking, use
on:click . You tell it which function to run:
n:click={handleClick}>
e
ndle all sorts of user actions like click , input , and mouseover .
handleClick(event) {
.log(event.clientX); // Shows where the cursor was on the x-axis
nent Events
ts can send out their own events with createEventDispatcher :
shout() {
h('shout', {
ge: 'Hello!'
ced Concepts
ores as a common space where any part of your app can pick up or
mation. There are mainly two types:
e stores - These are like bulletin boards where components can see the latest
perfect for displaying things like user settings or data from the internet.
stores - These are more interactive. Components can not only see the
ion but also change it. This is great for things that need to be updated often, like
s in a shopping cart.
increment() {
+= 1;
art is, any component using the store will automatically get the
lso other types of stores, like derived that can calculate new values
ther stores, and tweened for adding smooth animations to changes.
e Functions
ponents have special functions for different times in their life:
e, you can use onMount to grab data when the component first
sync () => {
esponse = await fetch('/api/data');
await response.json();
(() => {
terval(timer);
c Components
to switch between different parts of your app, you can use
omponent> with a variable to decide which part to show:
ntTab = Tab1;
omponent this={currentTab}>
nother way to insert changing content into a fixed structure, like tabs
content but keeping the same outer look:
e advanced features in Svelte can really help make your apps more
nd interactive.
g a Sample App
we're going to make a simple app for keeping track of tasks using
s will help us see how Svelte's parts work together.
Setup
create a new Svelte project and add some tools we need:
svelte@latest svelte-tasks
-tasks
ll date-fns uuid
g date-fns to handle dates and times, and uuid to create unique IDs for
nents
l have three parts:
patch = createEventDispatcher();
addTask() {
h('add', {
tails for the new task
handleAdd(event) {
= [...$tasks, event.detail];
s
ome cool things we could add to our app:
tasks
lter tasks
you go! With components, stores, and events, we can create a neat
ng app with Svelte.
y daily.dev Today
ct with developers around the world. Upgrade your developer journey with
alized content, collaboration, and an engaged community.
ucing SvelteKit
a tool that helps you make websites and web apps. It's built on Svelte,
alked about before, but it adds more features so you can do more
making your website work better for search engines and helping
faster.
t SvelteKit offers:
atures
e Rendering
elteKit, your web pages can be prepared on the server before they get to your
This means they can load faster and be more friendly to search engines.
ork on different platforms like Vercel, Netlify, and regular servers, thanks to its
setup.
t uses the files and folders in your project to figure out the web page structure.
ndle all sorts of web page setups, including ones that change based on the link
multiple layers.
ting
down your code into smaller parts so your website only loads what it needs for
Experience
uick updates to your work as you make changes, so you can see what you're
ght away.
with built-in support for TypeScript and makes it easy to use styles like Sass.
Extensibility
add custom features to your development setup. For example, checking your
great for making more complex websites and web apps because it
e Svelte in a way that's easy for search engines to read and quick for
ad. It simplifies a lot of the technical stuff, making it easier to get your
running.
ractices
Structure
your Svelte code in a src folder.
omponents by their use or which part of the site they belong to.
ting
space indent for nested elements.
your code with <script> , then the HTML markup, and <style> last.
nents
make components that you can use in many places, can be easily adjusted, and
ple.
ycle functions wisely to manage what happens when components load, update,
way.
ng pages that are rendered on the server, tools like Playwright are handy.
eKit take care of getting your project ready for people to use.
hese simple steps can help you work better with Svelte and SvelteKit.
ur code organized, easy to read, and well-tested makes a big
And remember, keeping your components small and your project
lear is key!
To Go From Here
ou've started with Svelte, there are many ways to keep learning and
ere's a guide on what you could do next:
dvanced Svelte
cial Svelte documentation is a great place to explore more about animations,
ender your app on the server, and how to test your code. They have lots of
xamples.
e challenging projects, look up some advanced tutorials like making a news app
also find lots of code samples and components on the Svelte REPL.
e Community
te Discord is a friendly space where you can talk to other developers and ask
s.
sveltejs hashtag.
ociety offers more learning resources like courses and articles from other
ers.
a Svelte meetup near you or online to meet other people interested in Svelte.
r Skills to Use
ing a bigger app with SvelteKit, and maybe use Node.js for the server side of
creating mobile apps, desktop apps, or browser extensions with Svelte Native.
feeling confident, you could even contribute to improving Svelte by helping with
or documentation.
’t forget to share what you make! Writing tutorials, making videos, or just
usion
great tool for making websites and apps that work really well and don't
your computer. Let's go over why Svelte is so good:
ce
akes your app's code small and fast right from the start.
t use a virtual DOM, which means it uses less computer memory and is quicker
u change something.
cludes the code you actually use, so your app isn't weighed down by extra stuff.
Experience
you write code for Svelte is straightforward and easy to pick up.
you helpful error messages, so you can fix problems before they get big.
tures like styles that only apply to parts of your app, smooth animations, and a
utomatically updates your app when data changes, which saves you time.
write CSS right in your components, and it won't mess up styles in other parts
app.
great set of tools for adding animations and dealing with user interactions.
t offers a smooth way to build and organize your app, making things like
a big community of Svelte users who make plugins, help out, and create guides.
orks well with other tools you might already be using, like TypeScript, ESLint,
tier.
use Svelte to build more than just web apps. It's also great for server-side
with Node.js.
ay to get better at Svelte is by making actual projects with it. Here are
s:
ot you can do with Svelte. Try making something and see how it goes.
do, share what you've made with others who are learning too!
d Questions
Read more
Nimrod Kramer
February 19, 2025
Changelog Contact
Status
🇮🇱 🇮🇹 🇵🇭 🇳🇱 🇬🇧 🇭🇷 🇱🇹 🇦🇺 🇵🇱 🇳🇴
Working remotely wherever we're happiest
🇦🇱 🇵🇹
© 2025 Daily
Terms Privacy Guidelines
Dev Ltd.