DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • 10 Svelte Data Grids: Choose the Right One for Your Project
  • Web Component Solutions: A Comparison
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • The Cypress Edge: Next-Level Testing Strategies for React Developers

Trending

  • ITBench, Part 1: Next-Gen Benchmarking for IT Automation Evaluation
  • Caching 101: Theory, Algorithms, Tools, and Best Practices
  • How to Format Articles for DZone
  • After 9 Years, Microsoft Fulfills This Windows Feature Request
  1. DZone
  2. Coding
  3. JavaScript
  4. How If-Else Logic works in Svelte

How If-Else Logic works in Svelte

Like other modern frameworks, such as Vue/React, Svelte allows for logic within components. Learn how if-else statement logic works in Svelte.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Updated Apr. 21, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
4.1K Views

Join the DZone community and get the full member experience.

Join For Free

Like other modern frameworks such as Vue and React, Svelte allows for logic within components. In this article, we'll look at how if and else statement logic works in Svelte outside of Javascript.

Using If-Else Statements in Svelte

Let's create a new component. Make a new file in your Svelte project in ./src/components called Component.svelte. Inside that component, add the following code:

JavaScript
 
<script>
    // we write export let to say that this is a property
    // that means we can change it later!
    export let x = 0;
    const addToCounter = function() {
        ++x;
    }
</script>

<button id="counter" on:click="{addToCounter}">{x}</button>

<style>
    button {
        background: #ffb334;
        border-radius: 8px;
        border: none;
        font-weight: bold;
        cursor: pointer;
        padding: 0.5rem 2rem;
        color: white;
        font-size: 1.5rem;
    }
</style>


This simple component creates a counter which adds 1 to its value every time you click it. Let's look at how we'd use inline logic in this example.

If Statement Logic in Svelte

Logic in Svelte goes inside {} curly brackets. Suppose we want a message to display based on the value of x from our component above. Our logic should be:

  • if the value is more than 5 and less than 10, show "more than 5 clicks!"
  • if the value is more than 10, show "wow that's a lot of clicks!"
  • otherwise, show "keep clicking!"

In Svelte, the syntax for that looks like this:

JavaScript
 
<script>
    export let x = 0;
    const addToCounter = function() {
        ++x;
    }
</script>

{#if x <= 10 && x > 5}
    <h2>more than 5 clicks!</h2>
{:else if x > 10}
    <h2>wow that's a lot of clicks!</h2>
{:else} 
    <h2>keep clicking!</h2>
{/if}

<button id="counter" on:click="{addToCounter}">{x}</button>


This means we don't have to rely on Javascript functions to write out which title we want to display. Instead, we can write it straight into our Svelte components.

If-Else Statements in Svelte

Similar to before, if you only wanted to have an if and an else, you can do that too:

{#if x <= 10 && x > 5}
    <h2>more than 5 clicks!</h2>
{:else} 
    <h2>keep clicking!</h2>
{/if}


And finally, an if statement alone also works - perfect for hiding or displaying DOM elements based on Javascript values:

{#if x <= 10 && x > 5}
    <h2>more than 5 clicks!</h2>
{/if}


Structure of If-Else Statements in Svelte

  • The first part of the if statement always starts with {#if STATEMENT} where STATEMENT is any logical Javascript statement we want to test out.
  • If we continue with else if or else, it's written as {:else if STATEMENT} and {:else} - using : instead of #.
  • We finish all logical statements with {/if}.
JavaScript Svelte

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 10 Svelte Data Grids: Choose the Right One for Your Project
  • Web Component Solutions: A Comparison
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • The Cypress Edge: Next-Level Testing Strategies for React Developers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!