Top Knockout JS Interview Questions (2021) | CodeUsingJava
















Most frequently Asked Knockout JS Interview Questions


  1. What is Knockout JS?
  2. What are the most prominent features of Knockout JS?
  3. What are the types of data binding Knockout JS supports?
  4. How can we activate a Knockout JS Model?
  5. What types of data binding are available in knockout JS?
  6. Describe ViewModel in Knockout Js?
  7. How is Knockout JS used in context properties?
  8. Explain how can we call ko.applyBindings to bind a partial view?
  9. How to debug template binding errors for KnockoutJS?
  10. What is indexOf?
  11. Give an example of indexOf?

What is Knockout JS?

Knockout JS is a MVC pattern used by developers in building rich and responsive websites, it is mostly used for designing and developing a SPA(Single Page Application).
Knockout JS is also used for reducing the amount of code needed for synchronizing data model and user interface control.It is fast, cross-browser compatible, and not reliant on any other libraries.


What are the most prominent features of Knockout JS?

Automatic UI Refresh
Dependency Tracking
Support all modern browsers
Template Binding
Declarative Binding
Compact Size less than 20 kb
Written in Pure JavaScript
Easy to implement


What are the types of data binding Knockout JS supports?

There are 2 types of data binding Knockout JS supports:
One-way binding
Two-way binding


How can we activate a Knockout JS Model?

For activating Knockout JS model we can use the code given below:
'ko.applyBindings(MyNewKOModel)'.  


What types of data binding are available in knockout JS?

Types of data binding are available in knockout JS are:
visible binding
text binding
value binding
css binding
style binding
attr binding
template binding


Describe ViewModel in Knockout Js?

ViewModel is a basic class that helps it as creating as JavaScript Function and is also declared as a variable that contains member variables and methods.

var myViewModel = {
personName: 'Bob',
personAge: function(age){
return  age+"years old";
}
};



How is Knockout JS used in context properties?

  • $parent property - helps in returning the current context item index in the array.
  • $index property - helps in allowing us to examine the parent of an object.

Explain how can we call ko.applyBindings to bind a partial view?


<div id="one">
  <input data-bind="value: name" />
</div>

<div id="two">
  <input data-bind="value: name" />
</div>

<script type="text/javascript">
  var viewModelA = {
     name: ko.observable("Bob")
  };

  var viewModelB = {
     name: ko.observable("Ted")
  };

  ko.applyBindings(viewModelA, document.getElementById("one"));
  ko.applyBindings(viewModelB, document.getElementById("two"));
</script>


How to debug template binding errors for KnockoutJS?

We can debug by using Chrome debugger :

<div data-bind="text: ko.toJSON($data)"></div>



What is indexOf?

IndexOf is a method that helps in returning the index of the first occurrence of the parameter provided.

What Is indexOf?


var filterAnArrayByArrayIndexOf = function (Text) {
  try {
      var filterText = Text.toLowerCase();        
      if (!filterText) {
          return selectionList();
      }
      else {
          if (selectionList().length > 0) {
              return ko.utils.arrayFilter(selectionList(), function (item) {
                  if (item.SelectionLabel.toLowerCase().indexOf(filterText) > -1 || item.SelectionDescription.toLowerCase().indexOf(filterText) > -1) {
                      return true;
                  }
                  else {
                     return false;
                  }
              });
          }
      }
  }
  catch (ex) {
      window.itk.utility.logExceptions(ex);
 }
};