Documentation Shento Hendriks
Svelte / SvelteKit
Svelte / SvelteKit
  • Getting Started with SvelteKit
    • Installing SvelteKit
      • Tailwind Typography plugin
    • Base Syntax & Core Features of Svelte
      • Curly Braces & Understanding Core Syntax
      • Reactive Variables
      • Two-way-binding Shortcut
      • Using multiple components
      • Components & Communication via Props
      • Display HTML in Svelte
      • Dynamic classes
      • Using $effect
      • Working with Javascript and Rune reactivity
    • Working with Conditionals & Loops
      • Showing code conditionally in Svelte
      • Looping through arrays (lists)
      • Lists & Keys
    • Closer look at reactivity
      • Updating Arrays & Objects
      • Event Modifiers
    • Component Events
    • Best Practices
    • Adding Google Font in Tailwind and SvelteKit
    • Adding Local Fonts to SvelteKit with Tailwind
    • Prevent flicker in images
    • Enhanced Images
    • Form Submissions in SvelteKit
    • Effective SEO in SvelteKit
    • Automatic Site Maps in Svelte
    • Creating Custom Elements with Svelte, SvelteKit, and Vite
    • Creating a Svelte Library with JavaScript Functions
  • Useful Code Snippets
    • Hover effects
    • Navigation
    • Centering Items
    • Setting Footer Bottom Page
  • Installing Useful Libraries
    • Documenting Javascript with JSDocs (Crash Course)
    • Adding Lottie Animations to Svelte Application
    • SvelteKit Melt-ui
      • First Component - accordion
    • Installing Shadcn/ui Svelte
    • Getting Started with GSAP with SvelteKit
      • What is GSAP?
      • Installing GSAP & Your first animation
      • GSAP in SvelteKit
    • Installing Locomotive Scroll with Svelte 5 and SvelteKit
  • SvelteKit Server Fetching and Deployment
    • Preparing a SvelteKit application for Deployment
    • Utility Functions for URL Handling in SvelteKit
    • Fetching Directus Data from Rest API with SvelteKit
    • Fetching data from api with caching
    • Displaying Fetched Data in a Each Loop
    • Creating slug friendly urls based on query data
    • Finding correct query in api based on url slug
Powered by GitBook
On this page
  1. Getting Started with SvelteKit
  2. Base Syntax & Core Features of Svelte

Curly Braces & Understanding Core Syntax

Curly Braces & Understanding Core Syntax in Svelte

In Svelte, single curly braces {} are used to output or run various elements such as functions, variables, and any single-line expressions. This guide will help you understand how to utilize curly braces effectively in your Svelte applications.

Basic Usage of Curly Braces

Curly braces in Svelte can be used to output:

  • Functions

  • Variables

  • Single-line expressions

Let's explore each of these with examples.

Functions and Variables

You can use curly braces to display the values of functions and variables directly in your HTML. Here’s a simple example:

<script>
    let name = "Shento";
    let age = $state(23);
    function increaseAge() {
        age += 1;
    }
</script>

<h1>Hello, my name is {name}, my age is {age}</h1>
<button onclick={increaseAge}>Increase my age</button>

Hello, my name is Shento, my age is 23

Increase my age

Explanation

  • name and age: These are variables that can be named anything you like.

  • increaseAge: This is a function that increments the age variable.

  • {name} and {age}: These curly braces are Svelte-specific syntax used to display the values of the variables in the HTML.

Single-Line Expressions

Curly braces can also be used to evaluate and display single-line expressions. Here’s an example:

<script>
    let name = "Shento";
    let age = 23;
</script>

<h1>Hello, my name is {name.toUpperCase()}, my age in 10 years is {age + 10}</h1>

Hello, my name is SHENTO, my age in 10 years is 33

Explanation

  • name.toUpperCase(): This is a JavaScript method that converts the name variable to uppercase.

  • age + 10: This is a simple arithmetic expression that adds 10 to the age variable.

  • {name.toUpperCase()} and {age + 10}: These curly braces are used to evaluate and display the expressions in the HTML.

Additional Information

Curly braces in Svelte are used to bind JavaScript expressions to the HTML, making it easy to dynamically display data.

Warning

Ensure that the expressions inside curly braces are single-line expressions. Multi-line expressions are not supported within curly braces.

Critical Alert

Avoid using complex logic inside curly braces. For better readability and maintainability, keep the logic in the script section and use curly braces only for simple expressions.

PreviousBase Syntax & Core Features of SvelteNextReactive Variables

Last updated 9 months ago