LaunchDarkly JavaScript Client SDK (Legacy)

3.9.1 · deprecated · verified Sun Apr 19

The `launchdarkly-js-client-sdk` package provides client-side feature flag management for JavaScript applications running in web browsers. It allows developers to control features remotely without redeploying code, enabling practices like A/B testing, gradual rollouts, and kill switches. This specific package is currently at version 3.9.1 but is officially deprecated. All future development and releases are now happening under the `@launchdarkly/js-client-sdk` monorepo package. It leverages the 'Client-side ID' for authentication and requires flags to be explicitly made available to client-side SDKs. Release cadence was previously frequent for this package, but it is now in maintenance mode, with new features directed to the new package. Its primary differentiator is robust feature flag management with detailed event tracking and a strong focus on enterprise features, often used in conjunction with React through the `react-client-sdk` addon.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the LaunchDarkly client for a given user, evaluates a boolean feature flag, and tracks a custom event, logging results to the console.

import { initialize, LDUser, LDClient, LDOptions } from 'launchdarkly-js-client-sdk';

// Replace with your actual Client-side ID from LaunchDarkly project settings
const LAUNCHDARKLY_CLIENT_SIDE_ID = process.env.LAUNCHDARKLY_CLIENT_SIDE_ID ?? 'YOUR_CLIENT_SIDE_ID';

// Define a user context. The 'key' is mandatory.
const user: LDUser = {
  key: 'example-user-key',
  name: 'Example User',
  email: 'user@example.com',
  custom: {
    region: 'us-west'
  }
};

// Define SDK options (optional, here enabling debug logging)
const options: LDOptions = {
  debug: true,
};

// Initialize the LaunchDarkly client
const ldClient: LDClient = initialize(LAUNCHDARKLY_CLIENT_SIDE_ID, user, options);

ldClient.on('ready', () => {
  console.log('LaunchDarkly client is ready!');

  // Evaluate a boolean feature flag with a default value of `false`
  const showNewFeature = ldClient.variation('new-feature-flag', false);

  if (showNewFeature) {
    console.log('The new feature is ENABLED for this user!');
  } else {
    console.log('The new feature is DISABLED for this user.');
  }

  // Track a custom event for analytics
  ldClient.track('button-click', { buttonId: 'submit-form' });
});

ldClient.on('failed', (error) => {
  console.error('LaunchDarkly initialization failed:', error);
});

// In a real application, you might export ldClient or make it globally available
// for other parts of your application to use.

view raw JSON →