Skip to main content

Multiple conditions can be 'chained' together with else if:

{#if x > 10}
	<p>{x} is greater than 10</p>
{:else if 5 > x}
	<p>{x} is less than 5</p>
{:else}
	<p>{x} is between 5 and 10</p>
{/if}
App.svelte
<script>
let x = 7;
</script>

{#if x > 10}
<p>{x} is greater than 10</p>
{:else}
<p>{x} is between 0 and 10</p>
{/if}

bundling https://unpkg.com/svelte@4.2.19/src/shared/utils/escape.js
/* App.svelte generated by Svelte v4.2.19 */
import {
SvelteComponent,
detach,
element,
empty,
init,
insert,
noop,
safe_not_equal
} from "svelte/internal";

import "svelte/internal/disclose-version";

function create_else_block(ctx) {
let p;

return {
c() {
p = element("p");
p.textContent = `${x} is between 0 and 10`;
},
m(target, anchor) {
insert(target, p, anchor);
},
p: noop,
d(detaching) {
if (detaching) {
detach(p);
}
}
};
}

// (5:0) {#if x > 10}
function create_if_block(ctx) {
let p;

return {
c() {
p = element("p");
p.textContent = `${x} is greater than 10`;
},
m(target, anchor) {
insert(target, p, anchor);
},
p: noop,
d(detaching) {
if (detaching) {
detach(p);
}
}
};
}

function create_fragment(ctx) {
let if_block_anchor;

function select_block_type(ctx, dirty) {
if (x > 10) return create_if_block;
return create_else_block;
}

let current_block_type = select_block_type(ctx, -1);
let if_block = current_block_type(ctx);

return {
c() {
if_block.c();
if_block_anchor = empty();
},
m(target, anchor) {
if_block.m(target, anchor);
insert(target, if_block_anchor, anchor);
},
p(ctx, [dirty]) {
if_block.p(ctx, dirty);
},
i: noop,
o: noop,
d(detaching) {
if (detaching) {
detach(if_block_anchor);
}

if_block.d(detaching);
}
};
}

let x = 7;

class App extends SvelteComponent {
constructor(options) {
super();
init(this, options, null, create_fragment, safe_not_equal, {});
}
}

export default App;
result = svelte.compile(source, {
generate:
css:
});
/* Add a <style> tag to see compiled CSS */