What does Svelte look like? Basic Svelte Code Examples

Search for a command to run...

No comments yet. Be the first to comment.
Assume we have 2 different sets of code written differently but solve the same problem and run like this: { Code 1 } # after execution: runtime: 3 seconds memory allocated: 20 GB { Code 2 } # after execution: runtime: 40 seconds memory allocated: 1 G...

Download and install the Google Chrome browser if you don't already have it installed Open the dev tools with F12 or by following the picture below For better control open the dev tools in a separate window Then you can fullscreen it and naviga...

A company's success or failure may be determined by the ability of a website to effectively communicate its message, encourage customers to do certain activities, and promote its products or services through various marketing channels. If a company ...

Learn python by finding a tutorial series in a form that fits your learning style and stick to it. It can be a video course, a book, or something in between. Apply what you are being taught and mix it up while you do so with examples of your own. To ...

I created this post for future reference, with my own examples as I was following along some basic Svelte tutorials that can be found here ( https://svelte.dev/tutorial/ )
<!-- App.svelte -->
<script>
let name = 'hashnode';
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
</script>
<h1>Hello {capitalizeFirstLetter(name)}!</h1>

<!-- App.svelte -->
<script>
let src = 'https://picsum.photos/200/200';
let alt = 'Some random image';
</script>
<h1>Image 1</h1>
<img alt={alt} src={src}>
<h1>Image 2</h1>
<img {alt} {src}>

<!-- App.svelte -->
<style>
span {
color: blue;
font-family: sans-serif;
}
</style>
<h1>Hi <span>Hashnode</span>!</h1>

<!-- App.svelte -->
<script>
import Nested from './Nested.svelte';
</script>
<style>
span {
color: blue;
font-family: sans-serif;
}
</style>
<h1>Hi <span>Hashnode</span>!</h1>
<Nested/>
<!-- Nested.svelte -->
<style>
span {
color: green;
}
</style>
<h2>Hello fellow <span>reader</span>!</h2>

<!-- App.svelte -->
<script>
import Nested from './Nested.svelte';
</script>
<style>
:global(span) {
color: blue;
font-family: sans-serif;
}
</style>
<h1>Hi <span>Hashnode</span>!</h1>
<Nested/>
<!-- Nested.svelte -->
<h2>Hello fellow <span>reader</span>!</h2>

<!-- App.svelte -->
<script>
let string = `Withount html tag <strong>HTML CONTENT!!!</strong>`;
let string2 = `With html tag <strong>HTML CONTENT!!!</strong>`;
</script>
<p>{string}</p>
<p>{@html string2}</p> <!-- this still needs sanitization -->
