Svelte: A quick Intro to JavaScript Frameworks

trinitymace

Mason Unuke

Posted on July 22, 2024

Svelte: A quick Intro to JavaScript Frameworks

Introduction

In growing landscape of JavaScript frameworks, Svelte stands out with its innovative approach. Unlike traditional frameworks like React and Vue, Svelte shifts the work from the browser to the build step, offering significant performance gains and a more intuitive development experience.

What is Svelte?

Svelte is a modern JavaScript framework created by Rich Harris. It takes a radically different approach by compiling your components into highly efficient imperative code that directly manipulates the DOM. This means no virtual DOM and no runtime overhead, resulting in faster and smaller applications.

Features of Svelte

  • No Virtual DOM: Svelte does things differently from other frameworks like React and Vue. These other frameworks use something called a virtual DOM to keep track of changes and update the webpage. This can be slow and use a lot of memory.

Svelte skips the virtual DOM and updates the webpage directly. This makes everything faster and uses less memory.
For example:

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

   <button on:click={() => count += 1}>
       Clicked {count} {count === 1 ? 'time' : 'times'}
   </button>
Enter fullscreen mode Exit fullscreen mode

In this example, when you click the button, the count goes up by 1. Svelte updates the webpage right away, so you see the number change immediately.

Learn more about the Virtual DOM

  • Reactive Declarations: Svelte makes it easy to keep things in sync with a special feature called reactive declarations. This means that when one thing changes, other things that depend on it update automatically. For example:
   <script>
       let a = 1;
       let b = 2;
       $: sum = a + b;
   </script>

   <p>{a} + {b} = {sum}</p>
Enter fullscreen mode Exit fullscreen mode

Here, a and b are numbers. The line $: sum = a + b; means that sum will always be the total of a and b. If you change a or b, the sum changes too, and Svelte updates the webpage to show the new total.

Understanding reactivity in Svelte

  • Simplicity and Readability of Svelte Components: Svelte components are easy to understand because everything you need is in one file. Each component has HTML, JavaScript, and CSS all together. This makes it easy to see how everything works. For example:
   <script>
       let name = 'world';
   </script>

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

   <p>Hello {name}!</p>
Enter fullscreen mode Exit fullscreen mode

In this example, the <script> section sets up a variable name. The <style> section adds some CSS to make the text purple. The HTML section uses the name variable to say "Hello" to whatever name you put in.

Svelte tutorial: building your first component

These key features show how Svelte makes building websites easier and faster. By directly updating the webpage, keeping things in sync automatically, and putting everything you need in one place, Svelte helps you create awesome web apps quickly and simply.

Advantages of Using Svelte

  • Performance Benefits: Due to its compilation step, Svelte applications have smaller bundle sizes and faster runtime performance compared to those using a virtual DOM.

  • Smaller Bundle Sizes: Since Svelte doesn't include a runtime, the final bundle is often much smaller.

  • Easier Learning Curve: Svelte’s syntax and structure are straightforward, making it accessible even for beginners.

Svelte vs. React: A Performance Comparison

Use Cases and Community

Svelte is suitable for a variety of applications, from small widgets to full-fledged web apps. Its community is growing rapidly, with numerous resources available.

Here are some popular projects Using Svelte

Here are some community and resources

Conclusion

Svelte represents a significant shift in how we build web applications, offering performance improvements and a delightful developer experience. Whether you're building a small widget or a large application, Svelte is worth considering for your next project. Dive into the documentation and explore the possibilities!

💖 💪 🙅 🚩
trinitymace
Mason Unuke

Posted on July 22, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related