Feature Flags in JavaScript: How to Enable Features Without Deployment

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Feature Flags in JavaScript: How to Enable Features Without Risk or Deployment

\n\n

Imagine: you've written a cool new feature — smart search with autocomplete. The code is ready, tests pass, but you're afraid to roll it out to all users at once. What if there's a bug? What if someone doesn't like it? In the past, you'd stay up all night deploying and then roll back versions. Today, there's an elegant solution — feature flags (or feature toggles).

\n\n

Feature flags are flags in code that allow you to enable or disable functionality without redeploying. Simply change a value in a config or admin panel — and the feature comes to life or disappears. In this article, we'll break down how to use them in JavaScript, show real code examples, and provide a checklist for implementation.

\n\n

What are feature flags and why are they needed in JS?

\n\n

Feature flags (also known as feature toggles) are simple conditional constructs that control whether to show a new feature to a user or not. They work at the code level: instead of deleting old code, you wrap the new code in a flag check.

\n\n

Why is this needed in JavaScript projects?

\n
    \n
  • Canary release — roll out a feature to 5% of users, monitor for errors, then expand.
  • \n
  • A/B testing — show different interface versions to different groups and compare metrics.
  • \n
  • Instant kill switch — if a feature breaks production, you simply disable the flag without deploying.
  • \n
  • Trunk-based development — you can merge incomplete code hidden behind a flag.
  • \n
\n\n

Simplest feature flag in JavaScript: example

\n\n

Let's start with the most basic option — a flag in a config. Suppose we have a search function, and we want to enable autocomplete only for testers.

\n\n
// config.js\nconst featureFlags = {\n  enableAutocomplete: false, // disabled by default\n  enableDarkMode: true\n};\n\n// search.js\nfunction initSearch() {\n  if (featureFlags.enableAutocomplete) {\n    console.log('Autocomplete enabled');\n    loadAutocompleteModule();\n  } else {\n    console.log('Autocomplete disabled');\n    loadBasicSearch();\n  }\n}\n
\n\n

Change enableAutocomplete to true in the config — and the feature is enabled. Simple? Yes. But this is just the beginning. For production, you need more — dynamic flags, API-based management, A/B tests.

\n\n

Advanced approach: libraries for feature flags

\n\n

You can write your own custom flag solutions, but it's better to use proven libraries. Here are the top 3 for JavaScript:

\n\n
    \n
  • LaunchDarkly — the corporate standard. Has SDKs for React, Vue, Node.js. Supports A/B testing, segmentation, percentage-based rollout.
  • \n
  • Flagsmith — an open-source alternative. Supports self-hosted and cloud. Works with any framework.
  • \n
  • Unleash — a lightweight open-source library. Great for smaller projects.
  • \n
\n\n

Example with Unleash:

\n\n
// Installation: npm install unleash-client\nconst { initialize } = require('unleash-client');\n\nconst unleash = initialize({\n  url: 'https://your-unleash-instance.com/api',\n  appName: 'my-js-app',\n  environment: 'production',\n  customHeaders: { Authorization: 'API-TOKEN' }\n});\n\n// Check flag\nconst isEnabled = unleash.isEnabled('new-checkout-flow');\nif (isEnabled) {\n  renderNewCheckout();\n} else {\n  renderOldCheckout();\n}\n
\n\n

Such libraries sync with the server in real time. If you change a flag on the dashboard, the client picks it up within a few seconds (usually 30-60 seconds, configurable).

\n\n

Feature flags in React: Hooks and context

\n\n

In React applications, it's convenient to use context and custom hooks. Here's an example architecture:

\n\n


                
                

Blogs

Book Recommendations