How to set if else knockout js conditions Magento 2?

You can use knockout if-else conditions in Magento 2 with knockout way or Magento 2 approaches.

You have to use ifnot for the else conditions. else is not supported in knockout.

ko if and ko ifnot use for if and else conditions.

Using Default Knockout JS
1. First Way

<!-- ko if: isDisplayed() -->
<span class="price">100</span>
<!-- /ko -->
<!-- ko ifnot: isDisplayed() -->
<span class="price">Guest User</span>
<!-- /ko -->

2. Second Way

<div data-bind="if: isDisplayed()">
    <span class="price">100</span>
</div>
<div data-bind="ifnot: isDisplayed()">
    <span class="price">Guest User</span>
</div>

If you have a function with a parameter,
<!– ko if: getCartParam(‘summary_count’) –>

Magento 2 Approach,
1. First Way

<if args="isDisplayed()">
    <span class="price">100</span>
</if>
<ifnot args="isDisplayed()">
    <span class="price">Guest User</span>
</ifnot>

2. Second Way,

<div if="isDisplayed()">
    <span class="price">100</span>
</div>
<div ifnot="isDisplayed()">
    <span class="price">Guest User</span>
</div>

If you have function with parameter and want to write Magento way,
<if args=”getCartParam(‘summary_count’)”>