Svelte Fundamentals

Loading concept...

🚀 Svelte Fundamentals: Your First Steps into the Magic Kingdom

Imagine you have a magic cookbook. You write a recipe once, and the cookbook transforms it into a perfectly baked cake—no oven needed! That’s Svelte.


🌟 What is Svelte?

Think of building websites like building with LEGO blocks.

Old way (React, Vue): You carry ALL the LEGO pieces to the playground. Your bag is heavy. At the playground, you build piece by piece while everyone waits.

Svelte way: A magic helper pre-builds your LEGO creation at home. You carry the finished toy—light and ready to play instantly!

The Simple Truth

Svelte is a tool that writes code for you.

You write simple instructions. Svelte transforms them into super-fast website code. Your website loads faster because there’s no extra “helper” running.

graph TD A[Your Svelte Code] --> B[Svelte Compiler] B --> C[Tiny, Fast JavaScript] C --> D[Happy Users!]

Why Should You Care?

Problem Svelte Solution
Slow websites Faster than others
Complex code Simple, readable
Big file sizes Small bundles
Hard to learn Easy to start

🔧 The Svelte Compiler Concept

What is a Compiler?

Imagine you speak English. Your friend speaks French. A translator sits between you, converting your words instantly.

The Svelte Compiler is your translator.

You write code the easy way. The compiler translates it into code browsers understand—perfectly optimized.

The Magic Happens BEFORE

Here’s the big difference:

Framework When Work Happens
React/Vue In browser (slow)
Svelte Before browser (fast)
graph TD A[Write Code] --> B[Build Time] B --> C{Svelte Compiler} C --> D[Optimized JavaScript] D --> E[Browser runs fast code]

Real Example

What you write:

<script>
  let count = 0;
</script>

<button on:click={() => count++}>
  Clicked {count} times
</button>

What browser gets: Super-optimized JavaScript that only updates what changed. No extra library needed!


📦 Single File Components

One File = One Complete Piece

Remember LEGO? Imagine if each LEGO piece came in a separate box. Confusing, right?

Svelte says: Everything for one piece goes in ONE file.

A .svelte file contains:

  • 🧠 Script (the brain)
  • 🎨 Markup (the face)
  • 💄 Style (the clothes)
graph TD subgraph "Button.svelte" A[Script Block] --> D[Complete Component] B[Markup Block] --> D C[Style Block] --> D end

File Structure

<script>
  // Brain: Logic goes here
</script>

<!-- Face: What users see -->
<button>Click me!</button>

<style>
  /* Clothes: How it looks */
  button { color: blue; }
</style>

One file. Three sections. Zero confusion.


🧠 Script Block

The Brain of Your Component

The <script> block is where you:

  • Store information (variables)
  • Do calculations
  • React to user actions

Basic Example

<script>
  let name = "Alex";
  let age = 10;

  function birthday() {
    age = age + 1;
  }
</script>

What Can Live in Script?

Item Example Purpose
Variables let count = 0 Store data
Functions function add() Do actions
Imports import X from Y Get helpers

Pro Tip: Reactivity is Automatic!

<script>
  let count = 0;
  // This updates automatically!
</script>

<p>Count is: {count}</p>
<button on:click={() => count++}>
  Add One
</button>

When count changes, the page updates. No extra code needed!


🎭 Markup Block

The Face Your Users See

Markup is the HTML part. It’s what appears on screen.

But Svelte markup has superpowers:

Superpower 1: Curly Braces {}

Put JavaScript values right in your HTML!

<script>
  let hero = "Spider-Man";
</script>

<h1>My hero is {hero}!</h1>

Output: My hero is Spider-Man!

Superpower 2: Logic Blocks

Show different things based on conditions:

<script>
  let score = 85;
</script>

{#if score >= 90}
  <p>Amazing! 🌟</p>
{:else if score >= 70}
  <p>Good job! 👍</p>
{:else}
  <p>Keep trying! 💪</p>
{/if}

Superpower 3: Loops

Show a list of things:

<script>
  let fruits = ["🍎", "🍌", "🍇"];
</script>

<ul>
  {#each fruits as fruit}
    <li>{fruit}</li>
  {/each}
</ul>

💄 Style Block

The Clothes That Only Fit One

Here’s something magical: Styles are scoped!

What does that mean? Imagine two kids both named “Sam.” In Svelte, when you call “Sam,” only YOUR Sam answers.

<style>
  p {
    color: red;
  }
</style>

<p>I am red!</p>

This red color ONLY affects <p> tags in THIS component. Other components stay unaffected!

Why This Matters

Old Way Svelte Way
Styles leak everywhere Styles stay contained
Need unique class names Use simple names
Fear of breaking things Change freely

Example: Safe Styling

<!-- Header.svelte -->
<style>
  h1 { color: blue; }
</style>
<h1>Blue Header</h1>

<!-- Footer.svelte -->
<style>
  h1 { color: green; }
</style>
<h1>Green Footer</h1>

Both files use h1, but each has its own color. No conflict!


📥 Component Import

Bringing Friends to the Party

Your website has many parts. Each part is a component. To use a component, you import it.

How to Import

<script>
  import Button from './Button.svelte';
  import Header from './Header.svelte';
</script>

<Header />
<Button />
graph TD A[App.svelte] --> B[Header.svelte] A --> C[Button.svelte] A --> D[Footer.svelte]

Step by Step

  1. Create a component file (e.g., Card.svelte)
  2. Import it in your script block
  3. Use it like an HTML tag
<script>
  import Card from './Card.svelte';
</script>

<!-- Use it like HTML! -->
<Card />
<Card />
<Card />

Three cards appear! One component, used many times.


📤 Component Export

Sharing Values with the World

Sometimes a component needs information from outside. That’s what props are for!

The export Keyword

<!-- Greeting.svelte -->
<script>
  export let name = "Friend";
</script>

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

Now the parent can pass data:

<!-- App.svelte -->
<script>
  import Greeting from './Greeting.svelte';
</script>

<Greeting name="Alice" />
<Greeting name="Bob" />
<Greeting />

Output:

  • Hello, Alice!
  • Hello, Bob!
  • Hello, Friend! (default)

Export = “I Accept Gifts!”

graph LR A[Parent] -->|"name"='Alice'| B[Greeting] B -->|Hello, Alice!| C[Screen]
Keyword Meaning
let My private variable
export let I accept this from parents

🎉 Putting It All Together

Here’s a complete Svelte component using everything:

<script>
  // Import another component
  import Badge from './Badge.svelte';

  // Accept props from parent
  export let playerName = "Hero";

  // Local state
  let score = 0;

  // Function
  function earnPoint() {
    score += 10;
  }
</script>

<!-- Markup with logic -->
<div class="player-card">
  <Badge />
  <h2>Player: {playerName}</h2>
  <p>Score: {score}</p>

  {#if score >= 100}
    <p class="winner">🏆 Champion!</p>
  {/if}

  <button on:click={earnPoint}>
    Earn Points!
  </button>
</div>

<style>
  .player-card {
    padding: 20px;
    border: 2px solid gold;
    border-radius: 10px;
  }

  .winner {
    font-size: 24px;
    color: gold;
  }

  button {
    background: purple;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
  }
</style>

🗺️ Quick Reference

Concept What It Does Example
Svelte Compiles to fast JS Framework
Compiler Transforms code Build step
Single File All in one .svelte Component
Script Logic & data <script>
Markup What users see HTML + {}
Style Scoped CSS <style>
Import Use other components import X
Export Accept data export let

🚀 You’re Ready!

You now understand the building blocks of Svelte:

  1. ✅ What makes Svelte special (compiler magic)
  2. ✅ Single file components (all-in-one)
  3. ✅ Script, Markup, and Style blocks
  4. ✅ How to import and export components

Next step? Start building! Create a Hello.svelte file and watch the magic happen.

Remember: Every expert was once a beginner. You’ve got this! 💪

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.