Logo
BlogDashboardAboutProjects
Logo
HomeBlogDashboardAboutProjects
GithubGithub
LinkedinLinkedin
TwitterTwitter
v1.12.1
29 Mar 2023
·
2 min read

Feature flags: ship code without shipping features

A feature flag is an if/else around a piece of code, controlled by an environment variable. It lets you merge and deploy work-in-progress without turning it on. Fewer conflicts, safer releases.

What are feature flags?

Feature flags allow developers to have more granular control of the features being released. It can be in a form of toggling a certain feature, enabling or disabling them. This is usually done with developers wrapping code around a particular feature.

Feature flags also help to reduce the risk of complicating the code base with potential conflicts.

An example would be a feature branch being worked on for a long time while the main branch was constantly updated with other features simultaneously. This could cause a possibility of a merge conflict when the stale branch gets merged.

As such, the feature flag allows us to frequently merge the changes while the feature is disabled when deployment into production is done.

Conducting experiments

Since feature flags can act as an "on/off" switch, A/B testing can be done easily to divide the traffic into different variants of the feature. Google Optimize offers A/B testing features to help with this.

How to create a feature flag?

Based on different requirements, there are many ways to create a feature flag. The following code is one example of how we can easily implement JavaScript.

FEATURE_FLAG_NEW_FEATURE=true
FEATURE_FLAG_NEW_FEATURE=false
if (process.env.FEATURE_FLAG_NEW_FEATURE === "true") {
  // Some cool features here
} else {
  // Some alternate features here
}

From the above example, we used the process.env environment variables to control a particular feature.

Conclusion

Feature flags are very powerful to give teams and especially developers more control of the features being released and in turn more control over the UX of a product.

Related Articles

21 Apr 2026
1 tag
Per-project shell environments with direnv
Loading the right environment variables when I cd into a project used to mean sourcing .env files by hand or leaking secrets into my global shell. direnv fixes both by loading a per-directory shell environment, and unloading it the moment I leave.
23 Jan 2023
1 tag
Adding JSON-LD structured data to your React blog
Google uses structured data to understand what's on your page. JSON-LD is the recommended format. Here's a minimal React component to add it, and how to verify it works.
11 Nov 2023
1 tag
process.env vs import.meta: environment variables in Node and the browser
process.env only runs in Node. It won't work in the browser. import.meta is the module-aware alternative, and it works with TypeScript too.
23 Nov 2023
1 tag
What the ! operator does in TypeScript and Swift
The ! at the end of a TypeScript expression tells the compiler you know the value isn't null or undefined. It's the same idea as force unwrapping in Swift. Useful, but worth understanding before reaching for it.