---
title: "Getting Started with Knockout.js: A Beginner's Guide"  
description: "Knockout.js is a JavaScript library that helps you create rich, responsive user interfaces with a clean underlying data model."  
author: "Ravi Vishwakarma"  
published: 2024-06-26  
updated: 2024-06-26  
canonical: https://www.mindstick.com/blog/304434/getting-started-with-knockout-js-a-beginner-s-guide  
category: "knockcout js"  
tags: ["web development", "knockout.js", "front-end development"]  
reading_time: 3 minutes  

---

# Getting Started with Knockout.js: A Beginner's Guide

[**Knockout.js**](https://www.mindstick.com/articles/1637/introduction-knockout-js) is a JavaScript library that helps you create rich, responsive [user interfaces](https://answers.mindstick.com/qa/102632/what-is-the-significance-of-google-s-material-design-for-user-interfaces) with a clean underlying data model. It follows the [**Model-View-ViewModel (MVVM) pattern**](https://www.mindstick.com/forum/157860/how-does-the-mvvm-pattern-work-in-knockoutjs-and-why-is-it-important), which provides a clear separation between the UI and the logic behind it. This guide will walk you through the basics of Knockout.js and how to get started with it.

#### What is Knockout.js?

Knockout.js is a standalone JavaScript implementation of the MVVM pattern that allows developers to:

- Create dynamic user interfaces with data-binding.
- [Simplify complex](https://www.mindstick.com/forum/160190/how-is-cte-used-to-simplify-complex-sql-queries) UIs by keeping the UI and data separate.
- Implement a [responsive design](https://www.mindstick.com/blog/303702/responsive-design-best-practices-examples) that automatically updates the UI when the data model changes.

#### Setting Up Your Environment

Before you can start using Knockout.js, you'll need to include it in your project. You can either download it or include it from a CDN.

#### Including Knockout.js from a CDN

You can include Knockout.js in your HTML file by adding the following script tag to the `<head>` or at the end of the `<body>`:

```html
    <!-- Link to Knockout.js library for MVVM pattern support -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.1/knockout-latest.min.js"
        integrity="sha512-vs7+jbztHoMto5Yd/yinM4/y2DOkPLt0fATcN+j+G4ANY2z4faIzZIOMkpBmWdcxt+596FemCh9M18NUJTZwvw=="
        crossorigin="anonymous" referrerpolicy="no-referrer"></script>
```

#### Creating a Simple Application

Let's create a simple application that allows you to manage a list of products. We'll cover the following steps:

1. Setting up the HTML structure.
2. Creating the ViewModel.
3. Binding the ViewModel to the View.

## Step 1: Setting Up the HTML Structure

Create an HTML file with the following structure:

```html
<!doctype html>
<html lang="en">

<head>
    <!-- Meta tags for character set and viewport configuration -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Title of the webpage -->
    <title>Bootstrap demo</title>

    <!-- Link to Bootstrap CSS for styling -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

    <!-- Link to Knockout.js library for MVVM pattern support -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.1/knockout-latest.min.js"
        integrity="sha512-vs7+jbztHoMto5Yd/yinM4/y2DOkPLt0fATcN+j+G4ANY2z4faIzZIOMkpBmWdcxt+596FemCh9M18NUJTZwvw=="
        crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>

<body>
    <!-- Main container using Bootstrap's grid system -->
    <div class="container">
        <!-- Header for the example -->
        <h2>Simple Knockout.js Example</h2>

        <!-- Section containing the interactive elements -->
        <div>
            <label>Name: </label>
            <!-- Text input bound to the 'name' observable -->
            <input id="nameInput" type="text" data-bind="value: name, valueUpdate: 'input'">
            <!-- Paragraph displaying the bound 'name' observable -->
            <p>Hello, <span data-bind="text: name"></span>!</p>
        </div>
    </div>

    <!-- JavaScript section defining the ViewModel and applying bindings -->
    <script>
        // ViewModel definition
        function AppViewModel() {
            // Observable property 'name' initialized with the value "World"
            this.name = ko.observable("John Doe");
        }

        // Apply Knockout.js bindings to the ViewModel instance
        ko.applyBindings(new AppViewModel());
    </script>
</body>

</html>
```

## Step 2: Creating the ViewModel

The ViewModel in Knockout.js acts as the intermediary between the View (HTML) and the Model (data). Here's the ViewModel for our product management application:

```javascript
// ViewModel definition
function AppViewModel() {
    // Observable property 'name' initialized with the value "World"
    this.name = ko.observable("John Doe");
}
```

## Step 3: Binding the ViewModel to the View

To connect the ViewModel to the View, you need to call the `ko.applyBindings` function and pass an instance of the ViewModel:

```javascript
// Apply Knockout.js bindings to the AppViewModel
ko.applyBindings(new AppViewModel());
```

#### Running the Application

1. Save the HTML file and open it in a web browser.
2. You should see a form to add new products and a table that displays the list of products.
3. Add, edit, and delete products using the form and buttons provided.

## Read more

[**Introduction Knockout.js**](https://www.mindstick.com/articles/1637/introduction-knockout-js)

[**Why Knockout js is not as popular as Angular JS?**](https://www.mindstick.com/articles/336197/why-knockout-js-is-not-popular-as-angular-js)

[**Form validation and request submission in knockout js**](https://www.mindstick.com/articles/335952/form-validation-and-request-submission-in-knockout-js)

[**Product Drag and Drop with Knockout**](https://www.mindstick.com/articles/335973/product-drag-and-drop-with-knockout)

[**Knockout.js: Building Dynamic Web Applications**](https://training.mindstick.com/courses/203/knockout-js-javascript-mvvc-framework-upcoming12)

---

Original Source: https://www.mindstick.com/blog/304434/getting-started-with-knockout-js-a-beginner-s-guide

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
