Introduction to observables and observable arrays
Ask by ICSM Computer
Updated 23 Apr 2025
Absolutely! Let's dive into the core concepts of Knockout.js:
observablesandobservableArrays. These are what give Knockout its reactive binding magic 🪄.What is an
observablein Knockout.js?An
observableis a special JavaScript object that tracks changes to a value. When the value changes, any UI elements or computed functions bound to it automatically update.Basic Syntax:
myObservable()myObservable("World")Example:
What is an
observableArray?An
observableArrayis just like a regular array, but Knockout tracks additions/removals and updates the UI accordingly.✅ Syntax:
Useful Methods:
push(item)remove(item)pop(),shift(),unshift()replace(oldItem, newItem)Example:
Example in HTML (Binding with UI)
Recap:
ko.observable()ko.observableArray()