Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Js

Idiom #205 Get an environment variable

Read an environment variable with the name "FOO" and assign it to the string variable foo. If it does not exist or if the system does not support environment variables, assign a value of "none".

const foo = process.env["FOO"] || "none";
const foo = process.env.FOO ?? 'none'

The nullish colaescing operator (??) is available in ES2020 onwards. It ensures that if the FOO environment variable is the empty string, foo isn't set to 'none'.
with Ada.Environment_Variables;
Foo : constant String :=
      Ada.Environment_Variables.Value (Name    => "FOO",
                                       Default => "none");

New implementation...
< >
tkoenig