MDN’s new design is in Beta! A sneak peek:

Scriptable access to the current process environment.
Inherits from: nsISupports Last changed in Gecko 1.7

Implemented by:;1 as a service:

var env = Components.classes[";1"].

Method overview

void set(in AString aName, in AString aValue);
AString get(in AString aName);
boolean exists(in AString aName);



Set the value of an environment variable.

 void set(
   in AString aName, 
   in AString aValue
The variable name to set.
The value to set.


Get the value of an environment variable.

 AString get(
   in AString aName

the variable name to retrieve.

Return value

Returns the value of the env variable. An empty string will be returned when the env variable does not exist or when the value itself is an empty string - please use exists() to probe whether the env variable exists or not.


Check the existence of an environment variable. This method checks whether an environment variable is present in the environment or not.

Note: * For Unix/Linux platforms we follow the Unix definition: An environment variable exists when getenv() returns a non-NULL value. An environment variable does not exist when getenv() returns NULL.

  • For non-Unix/Linux platforms we have to fall back to a "portable" definition (which is incorrect for Unix/Linux!!!!) which simply checks whether the string returned by Get() is empty or not.
 boolean exists(
   in AString aName
The variable name to probe.
Return value

If the variable has been set, the value returned is PR_TRUE. If the variable was not defined in the environment PR_FALSE will be returned.



This example gets the path to the Porgram Files directory on Windows. Credit to morat from mozillazine.

var nsIEnvironment = Components.classes[";1"].getService(Components.interfaces.nsIEnvironment);
var pathToProgramFiles = nsIEnvironment.get("ProgramFiles"); //for me this returns "C:\Program Files"


Document Tags and Contributors

 Contributors to this page: Noitidart, Sheppy, Wladimir_Palant, Nickolay, Aqualon, Ayhfung
 Last updated by: Noitidart,