> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Flag variation evaluation

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

This topic explains how to use the flag evaluation feature to serve different feature flag variations to contexts and users. This feature is available for all of the LaunchDarkly SDKs.

## About flag evaluation

The flag evaluation feature requires a feature flag key and the context that encounters the flag in your application. It returns the value of the feature flag variation for that context, based on the flag targeting rules you have created, including any [prerequisite flags](/docs/home/flags/prereqs).

Every flag has at least two variations: one for when targeting is off, and one for when it's on. To learn more, read [Creating flag variations](/docs/home/flags/variations).

This is an example of a flag with three variations:

<Frame caption="The &#x22;Variations&#x22; definition for multivariate flag.">
  <img src="https://mintcdn.com/launchdarkly/hutarVphEq2dY_zb/images/auto/experiment-example-opt-in-flag-variations.auto.png?fit=max&auto=format&n=hutarVphEq2dY_zb&q=85&s=4ddcd449e75b5639c037968c26275af1" alt="The &#x22;Variations&#x22; definition for multivariate flag." width="2026" height="1190" data-path="images/auto/experiment-example-opt-in-flag-variations.auto.png" />
</Frame>

Flags also have fallback values. The fallback value is defined in your code, is one of the flag's variations, and is only returned if an error occurs. For example, the SDK serves the fallback value if LaunchDarkly is unreachable, the feature flag key doesn't exist, or the context or user doesn't have a key specified. It also serves the fallback value if it cannot authenticate, or if your LaunchDarkly account has been deleted.

<Note>
  **Contexts without a context kind are interpreted as users**

  If you are working with an older version of the SDK, you may provide a user object in the evaluation call. If you are using an SDK that supports contexts, and don't supply the context kind, then that object is automatically interpreted as a context with a kind of "user." To learn more, read [Context kinds](/docs/home/flags/context-kinds).
</Note>

The flag evaluation feature adds a context to the **Contexts** list, if a context with the same key does not already exist. However, each SDK evaluates flags based only on the object you provide in the evaluation call. In other words, the SDK does not automatically use the attributes shown on the **Contexts** list, and attributes are not synchronized across SDK instances. You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly. To learn more, read [Context configuration](/docs/sdk/features/context-config).

You do not need to create contexts or users manually, but if you want to, you can with the identify feature. To learn more, read [Identifying and changing contexts](/docs/sdk/features/identify).

The flag evaluation feature *only* returns information about the flag variation for the context. To access other information about a flag, visit the [**Flags** list](/docs/home/flags/manage) in the LaunchDarkly user interface. Alternatively, use the [`getFeatureFlag` endpoint](/docs/api/feature-flags/get-feature-flag) in [LaunchDarkly's REST API](/docs/home/infrastructure/api).

Details about each SDK's configuration are available in the SDK-specific sections below:

* [Client-side SDKs](#client-side-sdks)
* [Server-side SDKs](#server-side-sdks)
* [Edge SDKs](#edge-sdks)

## Client-side SDKs

This feature is available for all of our client-side SDKs:

* [.NET (client-side)](#net-client-side)
* [Android](#android)
* [C++ (client-side)](#c++-client-side)
* [Electron](#electron)
* [Flutter](#flutter)
* [iOS](#ios)
* [JavaScript](#javascript)
* [Node.js (client-side)](#node-js-client-side)
* [React Native](#react-native)
* [React Server Component support](#react-server-component-support)
* [React Web](#react-web): The React Web SDK relies on the JavaScript SDK for this functionality.
* [Roku](#roku)
* [Vue](#vue)

### .NET (client-side)

<Accordion title="Expand .NET (client-side) code sample">
  The `Variation` method determines which variation of a flag LaunchDarkly serves to the current context. It requires the flag key and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  In the client-side .NET SDK, there is  a `variation` method for each type, such as `BoolVariation` or `StringVariation`.

  Here is an example:

  <CodeGroup>
    ```csharp title="C#" lines wrap theme={null}
    client.BoolVariation("example-flag-key", false);
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### Android

<Accordion title="Expand Android code sample">
  The `variation` method determines which variation of a flag LaunchDarkly serves to the current context. It requires the flag key and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list. When `LDClient` is initialized for the first time at app launch, contexts receive the feature flag fallback values until an initial connection to LaunchDarkly completes.

  In Android, there is  a `variation` method for each type, such as `boolVariation` or `stringVariation`.

  Here is an example:

  <CodeGroup>
    ```java title="Java" lines wrap theme={null}
    boolean variationResult = client.boolVariation(flagKey, false);
    ```

    ```kotlin title="Kotlin" lines wrap theme={null}
    val variationResult: Boolean = client.boolVariation(flagKey, false)
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### C++ (client-side)

<Accordion title="Expand C++ (client-side) code sample">
  The variation methods determine which variation of a flag is served to the current context. The variation method signatures take the feature flag key and a fallback value. When the client is initialized for the first time, contexts receive the feature flag fallback values until an initial connection to LaunchDarkly completes.

  There are variation methods for each type, such as `BoolVariation` or `StringVariation`.

  Here is an example:

  <CodeGroup>
    ```cpp title="C++ SDK v3.0 (native)" lines wrap theme={null}
    bool show_feature = client.BoolVariation("example-flag-key", false);
    if (show_feature) {
        // Application code to show the feature
    } else {
        // The code to run if the feature is off
    }
    ```

    ```c title="C++ SDK v3.0 (C binding)" lines wrap theme={null}
    bool show_feature = LDClientSDK_BoolVariation(client, "example-flag-key", false);
    if (show_feature) {
        // Application code to show the feature
    } else {
        // The code to run if the feature is off
    }
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

  To learn more, read the [`Client` documentation](https://launchdarkly.github.io/cpp-sdks/libs/client-sdk/docs/html/classlaunchdarkly_1_1client__side_1_1Client.html).
</Accordion>

### Electron

<Accordion title="Expand Electron code sample">
  The `variation` method determines which variation of a flag LaunchDarkly serves to the current context. It requires the flag key and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  To evaluate any feature flag for the current context, call `variation`:

  <CodeGroup>
    ```js title="JavaScript" lines wrap theme={null}
    const flagValue = client.variation('example-flag-key', false);

    // proceed based on flag value, for example:

    if (flagValue)  {
      // feature flag targeting is on
    } else {
      // feature flag targeting is off
    }
    ```

    ```ts title="TypeScript" lines wrap theme={null}
    const boolFlagValue = client.boolVariation('example-bool-flag-key', false);
    const numberFlagValue = client.numberVariation('example-number-flag-key', 2);
    const stringFlagValue = client.stringVariation('example-string-flag-key', 'default');

    // proceed based on flag value, for example:

    if (boolFlagValue)  {
      // feature flag targeting is on
    } else {
      // feature flag targeting is off
    }
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

  The return value of `variation` is always either one of the variations you defined for your flag in the flag's **Variations** tab, or the [fallback value](/docs/home/getting-started/vocabulary#fallback-value). The fallback value is the second parameter to `variation`.

  You can also fetch all flags for the current context.

  Here is an example:

  <CodeGroup>
    ```js title="JavaScript" lines wrap theme={null}
    const flags = client.allFlags();
    const flagValue = flags['example-flag-key'];
    ```
  </CodeGroup>

  This returns a key-value map of all your feature flags. It contains `null` values for any flags that could not be evaluated.

  Both of these methods are synchronous. The client always has the last known flag values in memory, so retrieving them does not involve any input/output (I/O).
</Accordion>

### Flutter

<Accordion title="Expand Flutter code sample">
  The `variation` methods determine which variation of a flag LaunchDarkly serves to the current context. `variation` calls take the feature flag key and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list. When the client is initialized for the first time at app launch, end users receive the feature flag fallback values until an initial connection to LaunchDarkly completes.

  In Flutter, there is  a `variation` method for each type, such as `boolVariation` or `stringVariation`.

  Here is an example:

  <CodeGroup>
    ```dart title="Flutter SDK v4" lines wrap theme={null}
    bool variationResult = client.boolVariation(flagKey, false);
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

  To learn more, read [`boolVariation`](https://pub.dev/documentation/launchdarkly_flutter_client_sdk/latest/launchdarkly_flutter_client_sdk/LDClient/boolVariation.html).
</Accordion>

### iOS

<Accordion title="Expand iOS code sample">
  The variation functions determine which variation of a flag LaunchDarkly serves the current context. Variation functions take the feature flag key and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value) as parameters. After the context is evaluated, you can view it on the **Contexts** list. If the flag does not exist, cannot be cast to the correct return type, or the `LDClient` is not started, the function returns the fallback value.

  In the iOS SDK, there is a variation method for each type, such as `boolVariation` or `jsonVariation`.

  Here is an example:

  <CodeGroup>
    ```swift title="Swift" lines wrap theme={null}
    let boolFlagValue = LDClient.get()!.boolVariation(forKey: "example-bool-flag-key", defaultValue: false)
    let jsonFlagValue = LDClient.get()!.jsonVariation(forKey: "json-flag-key-456def", defaultValue: ["enabled": false, "special": "none"])
    ```

    ```objectivec title="Objective-C" lines wrap theme={null}
    BOOL boolFlagValue = [[LDClient get] boolVariationForKey:@"example-bool-flag-key" defaultValue:NO];
    LDValue* jsonFlagValue = [[LDClient get] jsonVariationForKey:@"json-flag-key-456def" defaultValue:[LDValue ofNull]];
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### JavaScript

<Accordion title="Expand JavaScript code sample">
  The `variation` method determines which variation of a flag LaunchDarkly serves to the current context. `variation` calls take the feature flag key and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  Here is an example of the `variation` method:

  <CodeGroup>
    ```js title="JavaScript" lines wrap theme={null}
    client.variation('example-flag-key', false);
    ```

    ```ts title="TypeScript" lines wrap theme={null}
    const boolFlagValue = client.variation('example-bool-flag-key', false) as boolean;
    const numberFlagValue = client.variation('example-numeric-flag-key', 2) as number;
    const stringFlagValue = client.variation('example-string-flag-key', 'default') as string;
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

  In v4.x of the JavaScript SDK, you can also use typed methods to evaluate flags:

  <CodeGroup>
    ```ts title="JavaScript SDK, v4.x" lines wrap theme={null}
    const boolFlagValue = client.boolVariation('example-bool-flag-key', false);
    const numberFlagValue = client.numberVariation('example-numeric-flag-key', 2);
    const stringValue = client.stringVariation('example-string-flag-key', 'default');
    const jsonValue = client.jsonVariation('example-json-flag-key', '{}');
    ```
  </CodeGroup>
</Accordion>

### Node.js (client-side)

<Accordion title="Expand Node.js (client-side) code sample">
  The `variation` method determines which variation of a flag LaunchDarkly serves to the current context. It requires the flag key and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  Here is an example:

  <CodeGroup>
    ```js title="JavaScript" lines wrap theme={null}
    const value = client.variation('example-flag-key', false);
    ```

    ```ts title="TypeScript" lines wrap theme={null}
    const boolFlagValue = client.boolVariation('example-flag-key', false);
    const numberFlagValue = client.numberVariation('example-flag-key', 2);
    const stringFlagValue = client.stringVariation('example-flag-key', 'default');
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### React Native

<Accordion title="Expand React Native code sample">
  The variation method determines the flag variation to serve for a specific context.

  In React Native, there is a variation method for each type, such as `boolVariation` or `stringVariation`. In the React Native SDK version 10, there is also a hook for each type, such as `useBoolVariation` or `useStringVariation`.

  Variation calls take the feature flag key and a fallback value. When `ReactNativeLDClient` is initialized for the first time at app launch, end users receive feature flag fallback values until an initial connection to LaunchDarkly is completed. You are not required to specify a context when you initialize `ReactNativeLDClient`. If you do not specify a context initially, end users will also receive fallback values until you do specify a context by calling `identify()`. To learn more, read [Identifying and changing contexts](/docs/sdk/features/identify#react-native).

  Here is an example:

  <CodeGroup>
    ```js title="React Native SDK v10, using variation hooks" lines wrap theme={null}
    import { useBoolVariation, useNumberVariation, useStringVariation, useJsonVariation } from '@launchdarkly/react-native-client-sdk'

    const boolResult = useBoolVariation('example-bool-flag-key', false);
    const numResult = useNumberVariation('example-numeric-flag-key', 2);
    const stringResult = useStringVariation('example-string-flag-key', '');
    const jsonResult = useJsonVariation('example-json-flag-key', {});
    ```

    ```js title="React Native SDK v10, using variation methods" lines wrap theme={null}
    import { useLDClient } from '@launchdarkly/react-native-client-sdk'

    const client = useLDClient();
    let boolResult = client.boolVariation('example-bool-flag-key', false);
    let numResult = client.numberVariation('example-numeric-flag-key', 2);
    let stringResult = client.stringVariation('example-string-flag-key', '');
    let jsonResult = client.jsonVariation('example-json-flag-key', {});
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.

  To learn more, read [`boolVariation`](https://launchdarkly.github.io/js-core/packages/sdk/react-native/docs/classes/LDClientImpl.html#boolVariation).

  The SDK also includes an untyped method to determine the variation of a feature flag. To learn more, read [`variation`](https://launchdarkly.github.io/js-core/packages/sdk/react-native/docs/classes/LDClientImpl.html#variation). We recommend using the strongly typed variation methods, such as `boolVariation`, which perform type checks and handle type errors.
</Accordion>

### React Server Component support

You can use a React server client wrapper to evaluate flags in React Server Components. It supports variation calls as one of its core features. It is compatible with the Vercel edge SDK and the Node.js server-side SDK. To learn more, read [React Server Component support](/docs/sdk/client-side/react/react-web#react-server-component-support).

### React Web

<Accordion title="Expand React Web code sample">
  The React Web SDK relies on the [JavaScript SDK](#javascript) for this functionality.

  <Note>
    **Fallback values in the React Web SDK**

    The fallback variation for each flag is returned if an error occurs. For example, the SDK serves the fallback variation if LaunchDarkly is unreachable, the feature flag key doesn't exist, or the context or user doesn't have a key specified.

    In the LaunchDarkly user interface, on the flag's **Targeting** tab, a value for the "Fallback variation" information appears. However, if you are using the React Web SDK this displayed value may not match the value actually being served for the fallback variation:

    * A value for the fallback variation may appear in the UI even though you have not defined a fallback variation in your code. Specifically, the React Web SDK automatically uses the results of [getting all flags](/docs/sdk/features/all-flags#react-web) to determine the value of the fallback variation. In some cases, this means that the fallback value is undefined. For example, it may be undefined if the SDK has not finished initializing, or if LaunchDarkly is unreachable.
    * If you use the `useLDClient()` hook and evaluate flags with the underlying JavaScript SDK's [`variation()` method](#javascript), then you can define a fallback variation. However, the value displayed in the UI may not match the value of the fallback variation that you set in the `variation()` call.

    Rather than relying on the "Fallback variation" display in the UI, review your application code to determine the fallback variation that the SDK will serve if LaunchDarkly is unreachable.
  </Note>
</Accordion>

### Roku

<Accordion title="Expand Roku code sample">
  The `*variation` methods determines which variation of a flag LaunchDarkly serves to the current context. The calls require the flag key and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  In the Roku SDK, there is a variation method for each type, such as `boolVariation` and `intVariation`.

  Here is an example:

  <CodeGroup>
    ```brightscript title="BrightScript" lines wrap theme={null}
    ' typed variations
    myInt = launchDarkly.intVariation("example-flag-key", 123)

    myBool = launchDarkly.boolVariation("example-flag-key", false)

    myString = launchDarkly.stringVariation("example-flag-key", "hello world!")

    myObjectOrArray = launchDarkly.jsonVariation("example-flag-key", {"value": 123})

    ' generic variation
    myValue = launchDarkly.variation("example-flag-key", false)
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### Vue

<Accordion title="Expand Vue code sample">
  The Vue SDK provides a typed composable for each flag type: `useBoolVariation`, `useStringVariation`, `useNumberVariation`, and `useJsonVariation`. Each one accepts a flag key and a default value, and returns a readonly ref that updates when the flag value changes.

  Each composable also accepts a reactive flag key of either a `Ref<string>` or a getter function. A component can change which flag it evaluates without unmounting.

  Here is an example:

  <CodeGroup>
    ```html title="Vue SDK v3.0" lines wrap theme={null}
    <script setup lang="ts">
    import {
      useBoolVariation,
      useStringVariation,
      useNumberVariation,
      useJsonVariation,
    } from '@launchdarkly/vue-client-sdk'

    const showNewFeature = useBoolVariation('example-bool-flag-key', false)
    const theme = useStringVariation('example-string-flag-key', 'default')
    const maxItems = useNumberVariation('example-numeric-flag-key', 10)
    const config = useJsonVariation('example-json-flag-key', {})
    </script>

    <template>
      <div v-if="showNewFeature">The new feature is on.</div>
    </template>
    ```
  </CodeGroup>

  Each composable returns a `ref`. In a template you use the value directly, and in `<script setup>` you read it through `.value`.

  To change which flag a component evaluates at runtime, pass a `ref` or a getter function as the key:

  <CodeGroup>
    ```ts title="Reactive flag key, Vue SDK v3.0" lines wrap theme={null}
    import { ref } from 'vue'
    import { useBoolVariation } from '@launchdarkly/vue-client-sdk'

    const flagKey = ref('example-flag-key')
    const enabled = useBoolVariation(flagKey, false)

    flagKey.value = 'another-flag-key' // `enabled` re-evaluates automatically
    ```
  </CodeGroup>
</Accordion>

## Server-side SDKs

This feature is available for all of our server-side SDKs:

* [.NET (server-side)](#net-server-side)
* [Apex](#apex)
* [C++ (server-side)](#c++-server-side)
* [Erlang](#erlang)
* [Go](#go)
* [Haskell](#haskell)
* [Java](#java)
* [Lua](#lua)
* [Node.js (server-side)](#node-js-server-side)
* [PHP](#php)
* [Python](#python)
* [Ruby](#ruby)
* [Rust](#rust)

### .NET (server-side)

<Accordion title="Expand .NET (server-side) code sample">
  The `Variation` methods determine which variation of a flag LaunchDarkly serves to a specific context. The methods require the flag key, the [`Context`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.Context.html) to evaluate, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  In .NET, there is  a `Variation` method for each type:

  * [`BoolVariation`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.Server.LdClient.html#LaunchDarkly_Sdk_Server_LdClient_BoolVariation_)
  * [`DoubleVariation`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.Server.LdClient.html#LaunchDarkly_Sdk_Server_LdClient_DoubleVariation_)
  * [`FloatVariation`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.Server.LdClient.html#LaunchDarkly_Sdk_Server_LdClient_FloatVariation_)
  * [`IntVariation`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.Server.LdClient.html#LaunchDarkly_Sdk_Server_LdClient_IntVariation_)
  * [`JsonVariation`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.Server.LdClient.html#LaunchDarkly_Sdk_Server_LdClient_JsonVariation_)
  * [`StringVariation`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.Server.LdClient.html#LaunchDarkly_Sdk_Server_LdClient_StringVariation_).

  Here is an example:

  <CodeGroup>
    ```csharp title=".NET SDK v7.0+ (C#)" lines wrap theme={null}
    var value = client.BoolVariation("your.feature.key", context, false);
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### Apex

<Accordion title="Expand Apex code sample">
  The `variation` methods determine which variation of a flag LaunchDarkly serves to a specific user. In Apex, there is a `variation` method for each type, such as `boolVariation` or `stringVariation`. The methods take an `LDUser`, a feature flag key, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the user is evaluated, you can view it on the **Contexts** list.

  Here is an example:

  <CodeGroup>
    ```java title="Apex" lines wrap theme={null}
    Boolean value = client.boolVariation(user, 'your.feature.key', false);
    ```
  </CodeGroup>

  You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### C++ (server-side)

<Accordion title="Expand C++ (server-side) code sample">
  The `variation` family of functions determine which variation of a flag LaunchDarkly serves to a specific context. The functions require a context, feature flag key, and [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  In C++, there is a `variation` function for each type, such as `BoolVariation` or `StringVariation`.

  Here is an example:

  <CodeGroup>
    ```cpp title="C++ SDK v3.0 (native)" lines wrap theme={null}
    bool value = client.BoolVariation(context, "example-flag-key", false);
    ```

    ```c title="C++ SDK v3.0 (C binding)" lines wrap theme={null}
    bool value = LDServerSDK_BoolVariation(client, context, "example-flag-key", false);
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### Erlang

<Accordion title="Expand Erlang code sample">
  The `variation` function determines which variation of a flag LaunchDarkly serves to a specific context. The functions take a flag key, context, [fallback value](/docs/home/getting-started/vocabulary#fallback-value), and an instance tag. In the example below, the fallback value is false. The instance tag is optional. After the context is evaluated, you can view it on the **Contexts** list.

  <CodeGroup>
    ```text title="Erlang SDK v2.0+" lines wrap theme={null}
    Flag = ldclient:variation(<<"example-flag-key">>, #{key => <<"example-context-key">>}, false, your_instance)
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### Go

<Accordion title="Expand Go code sample">
  The `Variation` methods determine which variation of a flag LaunchDarkly serves to a specific context:

  * If you are working with an `LDScopedClient`, the context is the multi-context comprised of all contexts currently in the scoped client.
  * If you are working with the `LDClient`, the context is the one you pass to the `Variation` methods.

  <Warning>
    **LDScopedClient is in beta**

    `LDScopedClient` is in beta. It is still undergoing testing and active development. Its functionality may change without notice, including becoming backwards incompatible.
  </Warning>

  In either case, the `Variation` methods also take the feature flag key and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  In Go, there is a `Variation` method for each type:

  * [`BoolVariation`](https://pkg.go.dev/github.com/launchdarkly/go-server-sdk/v7#LDScopedClient.BoolVariation)
  * [`IntVariation`](https://pkg.go.dev/github.com/launchdarkly/go-server-sdk/v7#LDScopedClient.IntVariation)
  * [`Float64Variation`](https://pkg.go.dev/github.com/launchdarkly/go-server-sdk/v7#LDScopedClient.Float64Variation)
  * [`StringVariation`](https://pkg.go.dev/github.com/launchdarkly/go-server-sdk/v7#LDScopedClient.StringVariation)
  * [`JSONVariation`](https://pkg.go.dev/github.com/launchdarkly/go-server-sdk/v7#LDScopedClient.JSONVariation), which can be any JSON type.

  Here is an example:

  <CodeGroup>
    ```go title="Go SDK v7.13.4+, using LDScopedClient" lines wrap theme={null}
    result, _ := scopedClient.BoolVariation("example-flag-key", false)

    // result is now true or false depending on the setting of this boolean feature flag
    // LDScopedClient is in beta and may change without notice.
    ```

    ```go title="Go SDK v6+, using LDClient" lines wrap theme={null}
    result, _ := client.BoolVariation("example-flag-key", context, false)

    // result is now true or false depending on the setting of this boolean feature flag
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly. To learn more about constructing the context, read [Context configuration](/docs/sdk/features/context-config#go).

  If you are using OpenTelemetry, then instead of using the `Variation` method for each type, you must use the `VariationCtx` method for each type. For example, use `BoolVariationCtx` rather than `BoolVariation`. The methods are the same except that the `VariationCtx` methods also require a Go context parameter. This Go context is used in the hook implementation that provides OpenTelemetry support. To learn more, read [OpenTelemetry](/docs/sdk/features/opentelemetry-server-side#go).
</Accordion>

### Haskell

<Accordion title="Expand Haskell code sample">
  The `variation` family of functions determine which variation of a flag LaunchDarkly serves to a specific context. The functions take a `Client`, `Context`, feature flag key, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  In Haskell, there is a `variation` function for each type:

  * [`boolVariation`](https://launchdarkly.github.io/haskell-server-sdk/LaunchDarkly-Server-Client.html#v:boolVariation)
  * [`intVariation`](https://launchdarkly.github.io/haskell-server-sdk/LaunchDarkly-Server-Client.html#v:intVariation)
  * [`doubleVariation`](https://launchdarkly.github.io/haskell-server-sdk/LaunchDarkly-Server-Client.html#v:doubleVariation)
  * [`stringVariation`](https://launchdarkly.github.io/haskell-server-sdk/LaunchDarkly-Server-Client.html#v:stringVariation)
  * [`jsonVariation`](https://launchdarkly.github.io/haskell-server-sdk/LaunchDarkly-Server-Client.html#v:jsonVariation)

  Here is an example:

  <CodeGroup>
    ```haskell title="Haskell SDK v4.0" lines wrap theme={null}
    myBoolVariation <- boolVariation client "example-flag-key" context False
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly. To learn more about constructing the context, read [Context configuration](/docs/sdk/features/context-config#haskell).
</Accordion>

### Java

<Accordion title="Expand Java code sample">
  The `variation` methods determine which variation of a flag LaunchDarkly serves to a specific context. The `variation` methods take the feature flag key, an [`LDContext`](https://launchdarkly.github.io/java-core/lib/sdk/server/com/launchdarkly/sdk/LDContext.html), and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  In Java, there is a `variation` method for each type:

  * [`boolVariation`](https://launchdarkly.github.io/java-core/lib/sdk/server/com/launchdarkly/sdk/server/LDClient.html#boolVariation-java.lang.String-com.launchdarkly.sdk.LDContext-boolean-)
  * [`intVariation`](https://launchdarkly.github.io/java-core/lib/sdk/server/com/launchdarkly/sdk/server/LDClient.html#intVariation-java.lang.String-com.launchdarkly.sdk.LDContext-int-)
  * [`doubleVariation`](https://launchdarkly.github.io/java-core/lib/sdk/server/com/launchdarkly/sdk/server/LDClient.html#doubleVariation-java.lang.String-com.launchdarkly.sdk.LDContext-double-)
  * [`stringVariation`](https://launchdarkly.github.io/java-core/lib/sdk/server/com/launchdarkly/sdk/server/LDClient.html#stringVariation-java.lang.String-com.launchdarkly.sdk.LDContext-java.lang.String-)
  * [`jsonValueVariation`](https://launchdarkly.github.io/java-core/lib/sdk/server/com/launchdarkly/sdk/server/LDClient.html#jsonValueVariation-java.lang.String-com.launchdarkly.sdk.LDContext-com.launchdarkly.sdk.LDValue-), which can be of any JSON type.

  Here is an example:

  <CodeGroup>
    ```java title="Java SDK v6.0+" lines wrap theme={null}
    boolean value = client.boolVariation("example-flag-key", context, false);
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### Lua

<Accordion title="Expand Lua code sample">
  The `variation` family of functions determine which variation of a flag LaunchDarkly serves to a specific context. The functions take a context, feature flag key, and [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  In Lua, there is a `variation` function for each type, such as `boolVariation` or  `stringVariation`.

  Here is an example:

  <CodeGroup>
    ```lua title="Lua SDK v2" lines wrap theme={null}
    local value = client:boolVariation(context, "example-flag-key", false)
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly. To learn more, read [`boolVariation`](https://launchdarkly.github.io/lua-server-sdk/modules/launchdarkly-server-sdk.html#boolVariation).
</Accordion>

### Node.js (server-side)

<Accordion title="Expand Node.js (server-side) code sample">
  The `variation` method determines which variation of a flag LaunchDarkly serves to a specific context. `variation` calls take the feature flag key, an `LDContext`, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  Here is an example:

  <CodeGroup>
    ```js title="Node.js SDK v9.x" lines wrap theme={null}
    const value = await client.boolVariation('example-flag-key', context, false);
    ```
  </CodeGroup>

  In the Node.js server-side SDK, there is a `variation` method for each type, such as `boolVariation` or `stringVariation`. These typed methods return a `Promise` and are recommended over the generic `variation` method.

  You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### PHP

<Accordion title="Expand PHP code sample">
  The `variation` method determines which variation of a flag LaunchDarkly serves to a specific context. `variation` calls take the feature flag key, an `LDContext`, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  Here is an example:

  <CodeGroup>
    ```php title="PHP SDK v5.0+" lines wrap theme={null}
    $value = $client->variation($key, $context, false);
    ```
  </CodeGroup>

  You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.
</Accordion>

### Python

<Accordion title="Expand Python code sample">
  The `variation` method determines which variation of a flag LaunchDarkly serves to a specific context. `variation` calls take the feature flag key, a `Context`, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  Here is an example:

  <CodeGroup>
    ```python title="Python SDK v8.0+" lines wrap theme={null}
    show_feature = ldclient.get().variation("your.feature.key", context, False)
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly. To learn more about constructing the context, read [Context configuration](/docs/sdk/features/context-config#python).
</Accordion>

### Ruby

<Accordion title="Expand Ruby code sample">
  The `variation` method determines which variation of a flag LaunchDarkly serves to a specific context. `variation` calls take the feature flag key, an `LDContext` or user hash, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  Here is an example:

  <CodeGroup>
    ```ruby title="Ruby SDK v7.0+" lines wrap theme={null}
    value = client.variation("your.feature.key", context, false)
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly. To learn more about constructing the context, read [Context configuration](/docs/sdk/features/context-config#ruby).
</Accordion>

### Rust

<Accordion title="Expand Rust code sample">
  The `variation` methods determine which variation of a flag LaunchDarkly serves to a specific context. `variation` methods take a `Context`, the feature flag key, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  In Rust, there is a `variation` method for each type:

  * [`bool_variation`](https://docs.rs/launchdarkly-server-sdk/latest/launchdarkly_server_sdk/struct.Client.html#method.bool_variation),
  * [`int_variation`](https://docs.rs/launchdarkly-server-sdk/latest/launchdarkly_server_sdk/struct.Client.html#method.int_variation),
  * [`float_variation`](https://docs.rs/launchdarkly-server-sdk/latest/launchdarkly_server_sdk/struct.Client.html#method.float_variation),
  * [`str_variation`](https://docs.rs/launchdarkly-server-sdk/latest/launchdarkly_server_sdk/struct.Client.html#method.str_variation) for strings, and
  * [`json_variation`](https://docs.rs/launchdarkly-server-sdk/latest/launchdarkly_server_sdk/struct.Client.html#method.json_variation), which can be any JSON type.

  Here is an example:

  <CodeGroup>
    ```rust title="Rust SDK v1+" lines wrap theme={null}
    let result = client.bool_variation(&context, "your.feature.key", false);
    // result is now true or false depending on the setting of this boolean feature flag
    ```
  </CodeGroup>

  You must provide all relevant context attributes for each evaluation for your targeting rules to apply correctly. To learn more about constructing the context, read [Context configuration](/docs/sdk/features/context-config#rust).
</Accordion>

## Edge SDKs

This feature is available for all of our edge SDKs:

* [Akamai](#akamai)
* [Cloudflare](#cloudflare)
* [Fastly](#fastly)
* [Vercel](#vercel)

### Akamai

<Accordion title="Expand Akamai code sample">
  The `variation` method determines which variation of a feature flag a context receives. `variation` calls take the feature flag key, an `LDContext`, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  Here is an example:

  <CodeGroup>
    ```typescript title="TypeScript" lines wrap theme={null}
    const flagValue = await client.variation('example-flag-key', context, false);
    ```
  </CodeGroup>

  You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.

  To learn more, read [`variation`](https://launchdarkly.github.io/js-core/packages/sdk/akamai-edgekv/docs/interfaces/LDClient.html#variation). The `LDClient` also provides typed variation methods for type-safe usage in TypeScript: [`boolVariation`](https://launchdarkly.github.io/js-core/packages/sdk/akamai-edgekv/docs/interfaces/LDClient.html#boolVariation), [`stringVariation`](https://launchdarkly.github.io/js-core/packages/sdk/akamai-edgekv/docs/interfaces/LDClient.html#stringVariation), [`numberVariation`](https://launchdarkly.github.io/js-core/packages/sdk/akamai-edgekv/docs/interfaces/LDClient.html#numberVariation), [`jsonVariation`](https://launchdarkly.github.io/js-core/packages/sdk/akamai-edgekv/docs/interfaces/LDClient.html#jsonVariation).

  Every time you evaluate a flag, the SDK fetches the flag data from the EdgeKV store. Your Akamai resource tier may limit how many of these queries you can make while a single worker handler is being executed. To learn more, read [Understand resource limits and caching options](/docs/sdk/edge/akamai#understand-resource-limits-and-caching-options).
</Accordion>

### Cloudflare

<Accordion title="Expand Cloudflare code sample">
  The `variation` method determines which variation of a feature flag a context receives. `variation` calls take the feature flag key, an `LDContext`, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  Here is an example:

  <CodeGroup>
    ```typescript title="TypeScript" lines wrap theme={null}
    const flagValue = await client.variation('example-flag-key', context, false);
    ```
  </CodeGroup>

  You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.

  To learn more, read [`variation`](https://launchdarkly.github.io/js-core/packages/sdk/cloudflare/docs/classes/LDClient.html#variation). The `LDClient` also provides typed variation methods for type-safe usage in TypeScript: [`boolVariation`](https://launchdarkly.github.io/js-core/packages/sdk/cloudflare/docs/classes/LDClient.html#boolVariation), [`stringVariation`](https://launchdarkly.github.io/js-core/packages/sdk/cloudflare/docs/classes/LDClient.html#stringVariation), [`numberVariation`](https://launchdarkly.github.io/js-core/packages/sdk/cloudflare/docs/classes/LDClient.html#numberVariation), [`jsonVariation`](https://launchdarkly.github.io/js-core/packages/sdk/cloudflare/docs/classes/LDClient.html#jsonVariation).
</Accordion>

### Fastly

<Accordion title="Expand Fastly code sample">
  The `variation` method determines which variation of a feature flag a context receives. `variation` calls take the feature flag key, an `LDContext`, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  Here is an example:

  <CodeGroup>
    ```ts title="TypeScript" lines wrap theme={null}
    const flagValue = await client.variation('example-flag-key', context, false);
    ```
  </CodeGroup>

  You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.

  To learn more, read [`variation`](https://launchdarkly.github.io/js-core/packages/sdk/fastly/docs/classes/LDClient.html#variation).

  The `LDClient` also provides typed variation methods for type-safe usage in TypeScript: [`boolVariation`](https://launchdarkly.github.io/js-core/packages/sdk/fastly/docs/classes/LDClient.html#boolVariation), [`stringVariation`](https://launchdarkly.github.io/js-core/packages/sdk/fastly/docs/classes/LDClient.html#stringVariation), [`numberVariation`](https://launchdarkly.github.io/js-core/packages/sdk/fastly/docs/classes/LDClient.html#numberVariation), [`jsonVariation`](https://launchdarkly.github.io/js-core/packages/sdk/fastly/docs/classes/LDClient.html#jsonVariation).
</Accordion>

### Vercel

<Accordion title="Expand Vercel code sample">
  The `variation` method determines which variation of a feature flag a context receives. `variation` calls take the feature flag key, an `LDContext`, and a [fallback value](/docs/home/getting-started/vocabulary#fallback-value). After the context is evaluated, you can view it on the **Contexts** list.

  Here is an example:

  <CodeGroup>
    ```typescript title="TypeScript" lines wrap theme={null}
    const flagValue = await client.variation('example-flag-key', context, false);
    ```
  </CodeGroup>

  You must provide all relevant attributes for each evaluation for your targeting rules to apply correctly.

  To learn more, read [`variation`](https://launchdarkly.github.io/js-core/packages/sdk/vercel/docs/classes/LDClient.html#variation). The `LDClient` also provides typed variation methods for type-safe usage in TypeScript: [`boolVariation`](https://launchdarkly.github.io/js-core/packages/sdk/vercel/docs/classes/LDClient.html#boolVariation), [`stringVariation`](https://launchdarkly.github.io/js-core/packages/sdk/vercel/docs/classes/LDClient.html#stringVariation), [`numberVariation`](https://launchdarkly.github.io/js-core/packages/sdk/vercel/docs/classes/LDClient.html#numberVariation), [`jsonVariation`](https://launchdarkly.github.io/js-core/packages/sdk/vercel/docs/classes/LDClient.html#jsonVariation).
</Accordion>
