Glossary · Programming

Environment Variable

An environment variable is a named value set outside a program's code, such as a port or API key, that the program reads from its operating environment.

Updated October 6, 2026

An environment variable is a key-value pair provided to a process by its surrounding environment, such as the shell, container or hosting platform, instead of being written into the source code. Programs read them at startup to learn their settings.

Why it matters

Keeping configuration out of code lets the same build run in development, staging and production with different settings. It also keeps secrets such as database passwords and API keys out of version control. Hosting dashboards, Docker and CI/CD systems all provide ways to set them.

Example

$ export DATABASE_URL="postgres://app:secret@db:5432/shop"
$ node server.js
const url = process.env.DATABASE_URL;
const port = process.env.PORT || 3000;

If PORT is not set, the code falls back to 3000.

Common mistakes

  • Committing a .env file that holds real secrets to the repository.
  • Forgetting to set the variable on the server after deploying, so the app starts with a missing value.
  • Printing all variables in logs or error pages, which leaks credentials.

Get notified when this term updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.