---
title: Forced Decision methods for the JavaScript (Node) SDK prior to v6
slug: feature-experimentation/docs/forced-decision-methods-javascript-node-prior-to-v6
docTags: 
createdAt: 2026-08-05T20:39:51.000Z
---

These methods help test and debug various flows of your client applications by forcing users into a specific variation.

The JavaScript Node SDK checks forced decisions before making any decisions. If a matching item is found for the requested flag, the SDK returns the forced decision immediately ([audience conditions](docId\:UDBvr5a8WHNkyOqzPhOr9) and traffic allocations are ignored) before making normal decisions.

The JavaScript Node SDK checks for forced decisions at the start of each decision process. If a matching forced decision is found, it returns the decision immediately.

- **Flag-to-Decision** – The SDK checks at the start of any decide call for the given flag.
- **Experiment-Rule-to-Decision** – The SDK checks at the start of the decision for the given experiment rule of the flag key.
- **Delivery-Rule-to-Decision** – The SDK checks at the start of the decision for the given delivery rule of the flag key.

:::hint{type="danger"}
**Warning**
You must associate your variations to a [flag rule](docId:-yq2-thNRv7-SgwCXpG90) before calling any Forced Decision methods.
:::

On forced decisions, the SDK fires impression events and notifications just like other normal decisions (unless disabled by the [disableDecisionEvent](docId\:OR0QUdFTq9bg4lGgOz-nH) option).

:::hint{type="info"}
**Note**
These forced decisions are not persistent and are cleared when the [OptimizelyUserContext](docId\:jEgEOt1Ywg2IFdWasBps7) is re-initialized.
:::

For information about each method, click on the method name.

- [setForcedDecision](docId\:W54yp5v6sAJ7qgtGhaa1P)
- [getForcedDecision](docId\:W54yp5v6sAJ7qgtGhaa1P)
- [removeForcedDecision](docId\:W54yp5v6sAJ7qgtGhaa1P)
- [removeAllForcedDecisions](docId\:W54yp5v6sAJ7qgtGhaa1P)

### OptimizelyDecisionContext

```javascript
export interface OptimizelyDecisionContext {
  flagKey: string;
  ruleKey?: string;
}
```

### OptimizelyForcedDecision

```javascript
export interface OptimizelyForcedDecision {
  variationKey: string;
}
```

## Set forced decision method - setForcedDecision()

### Version

4.9.0

### Description

Sets a forced decision (`variationKey`) for a given `OptimizelyDecisionContext`.

### Parameters

This table lists the required and optional parameters for the Javascript SDK.

| Parameter                  | Type      | Description                                                                                                                                  |
| -------------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\*   | Interface | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to set. |
| **decision**\ \*required\* | Interface | An instance of \`OptimizelyForcedDecision\` with the required \`variationKey\` for the forced decision you want to set.                      |

### Returns

A boolean value that indicates if setting the forced decision (`variationKey`) was completed.

### Example

See the full Javascript SDK example [here](docId\:W54yp5v6sAJ7qgtGhaa1P).

## Get forced decision method - getForcedDecision()

### Version

4.9.0

### Description

Returns the forced decision (`variationKey`) for a given `OptimizelyDecisionContext`. Returns the `OptimizelyForcedDecision` instance or null if there is no matching item.

### Parameters

This table lists the required and optional parameters for the Javascript SDK.

| Parameter                | Type      | Description                                                                                                                                  |
| ------------------------ | --------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\* | Interface | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to get. |

### Returns

A forced decision `OptimizelyForcedDecision` instance for the context or null if there is no matching item.

### Example

See the full Javascript SDK example [here](docId\:W54yp5v6sAJ7qgtGhaa1P).

## Remove forced decision method - removeForcedDecision()

### Version

4.9.0

### Description

Removes the forced decision (`variationKey`) for a given `OptimizelyDecisionContext`.

### Parameters

This table lists the required and optional parameters for the Javascript SDK.

| Parameters               | Type   | Description                                                                                                                                     |
| ------------------------ | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| **context** \*required\* | struct | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to remove. |

### Returns

A success/failure boolean status if the forced decision (`variationKey`) was removed.

### Example

See the full Javascript SDK example [here](docId\:W54yp5v6sAJ7qgtGhaa1P).

## Remove all forced decisions method - removeAllForcedDecisions()

### Version

4.9.0

### Description

Removes all forced decisions (`variationKey`) for the user context.

### Parameters

This table lists the required and optional parameters for the Javascript SDK.

| Parameters | Type | Description |
| ---------- | ---- | ----------- |
| None       | N/A  | N/A         |

### Returns

A success/failure boolean status.

### Example

See the full Javascript SDK example [here](docId\:W54yp5v6sAJ7qgtGhaa1P).

## Full Code Example

```javascript
const { createInstance } = require('@optimizely/optimizely-sdk');

const optimizely = createInstance({
  sdkKey: '<YOUR_SDK_KEY>'// Provide the sdkKey of your desired environment here
});

if (optimizely) {
  optimizely.onReady().then(({ success, reason }) => {
    if (!success) {
      throw new Error(reason);
    }

    const attributes = { logged_in: true };
    const user = optimizely.createUserContext('user123', attributes);
    if (!user) {
      throw new Error('failed to create user context');
    }

    let result, decision, forcedDecision;

    // set a forced decision for a flag
    result = user.setForcedDecision({flagKey: 'flag-1'}, {variationKey: 'off'});
    decision = user.decide('flag-1');

    // set a forced decision for an ab-test rule
    result = user.setForcedDecision({flagKey: 'flag-2', ruleKey: 'ab-test-1'}, {variationKey: 'variation-b'});
    decision = user.decide('flag-2');

    // set a forced variation for a delivery rule
    result = user.setForcedDecision({flagKey: 'flag-3', ruleKey: 'delivery-1'}, {variationKey: 'on'});
    decision = user.decide('flag-3');

    // get forced variations
    forcedDecision = user.getForcedDecision({flagKey: 'flag-2'});

    // remove forced variations
    result = user.removeForcedDecision({flagKey: 'flag-2', ruleKey: 'ab-test-1'});
    user.removeAllForcedDecisions();
    user.decideAll();

  }).catch((err) => {
    console.log(err);
    // handle error
  });
} else {
  // handle instantiation error
}
```

## See Also

[OptimizelyUserContext](docId\:jEgEOt1Ywg2IFdWasBps7)
