Build a Simple Counter App in Svelte

Build a Simple Counter App in Svelte

05/22 By frontendshape

Hello Friend, Today in this section we will build a simple counter app (increment and decrement) using svelte.

How to install & setup Vite + Typescript in Svelte


Svelte Counter App Example

1) Build a simple counter app using svelte inline click events handler.

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

<button on:click={() => count -= 1}>-</button>
<span>{ count }</span>
<button on:click={() => count += 1}>+</button>


2) Counter app using svelte click events.

<script>
	let count = 0;
	function increment(){
		count += 1;
	}
	
	function decrement() {
		count -= 1;
	}
</script>

<button on:click={increment}>-</button>
<span>{ count }</span>
<button on:click={decrement}>+</button>
counter app with svelte

counter app with svelte


Enter Your Search Query