1. Home
  2. /
  3. Knockout-js
  4. /
  5. Knockout.js Counter with Double Increment

Knockout.js Counter with Double Increment - Knockout-js Typing CST Test

Loading…

Knockout.js Counter with Double Increment — Knockout-js Code

Counter that increments by 2 on a special button.

<div class="container" data-bind="css: isDark() ? 'dark-theme' : 'light-theme'">
	<h2>Counter: <span data-bind="text: count"></span></h2>
	<div>
		<button data-bind="click: increment">+</button>
		<button data-bind="click: doubleIncrement">++</button>
		<button data-bind="click: decrement">-</button>
		<button data-bind="click: reset">Reset</button>
	</div>
	<button data-bind="click: toggleTheme">Toggle Theme</button>
</div>

<script>
function CounterViewModel() {
	this.count = ko.observable(0);
	this.isDark = ko.observable(false);

	this.increment = () => { this.count(this.count() + 1); };
	this.doubleIncrement = () => { this.count(this.count() + 2); };
	this.decrement = () => { this.count(this.count() - 1); };
	this.reset = () => { this.count(0); };
	this.toggleTheme = () => { this.isDark(!this.isDark()); };
}
ko.applyBindings(new CounterViewModel());
</script>

Knockout-js Language Guide

Knockout.js is a lightweight JavaScript library that uses the MVVM (Model-View-ViewModel) pattern to create responsive, data-driven UIs with declarative bindings. It emphasizes simplicity, observables, and automatic UI updates.

Primary Use Cases

  • ▸Data-driven dashboards
  • ▸Forms with heavy user interaction
  • ▸Enterprise legacy SPA systems
  • ▸MVVM-style large UI logic
  • ▸Low-dependency applications where frameworks like React/Vue are unnecessary

Notable Features

  • ▸Observable variables and computed observables
  • ▸Declarative two-way bindings
  • ▸Automatic UI refresh
  • ▸Dependency tracking engine
  • ▸Templating and custom bindings

Origin & Creator

Created by Steve Sanderson at Microsoft in 2010 to simplify dynamic web UIs using the MVVM pattern.

Industrial Note

Knockout.js is still used in long-lived enterprise systems, internal dashboards, and large legacy codebases due to its stability, backward compatibility, and lightweight footprint.

More Knockout-js Typing Exercises

Knockout.js Simple CounterKnockout.js Counter with StepKnockout.js Counter with Min/MaxKnockout.js Counter with HistoryKnockout.js Counter with Auto IncrementKnockout.js Counter with Even/Odd IndicatorKnockout.js Counter with LocalStorageKnockout.js Counter with Color Themes

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher