Variable (Global Object)
→ objectThe global Variable object provides access to AMPscript variables from SSJS, enabling bidirectional data exchange between the two scripting languages.
Available in:
Email
CloudPage
Automation
Triggered Send
Syntax
Variable.GetValue(name)
Variable.SetValue(name, value)
1–2 arguments
Methods
| Method | Returns | Description |
|---|---|---|
Variable.GetValue(name) |
string | Gets the value of an AMPscript variable |
Variable.SetValue(name, value) |
void | Sets the value of an AMPscript variable |
Description
The global Variable object provides the bridge between AMPscript and SSJS. AMPscript variables are prefixed with @ and live in a shared scope accessible by both languages on the same page.
This is the same as Platform.Variable.GetValue() and Platform.Variable.SetValue().
Examples
Read an AMPscript variable
%%[
SET @subscriberKey = _subscriberKey
SET @emailAddress = emailaddr
]%%
<script runat="server">
// Read variables set by AMPscript
var sk = Variable.GetValue("@subscriberKey");
var email = Variable.GetValue("@emailAddress");
Write("<p>Processing: " + email + "</p>");
</script>
Write an AMPscript variable
<script runat="server">
var data = Platform.Function.Lookup("Preferences", "Theme", "SubscriberKey", sk);
Variable.SetValue("@theme", data || "light");
</script>
<!-- Use the variable in AMPscript -->
<body class="theme-%%=v(@theme)=%%">
Bridge pattern for AMPscript functions
The most important use: safely pass values into AMPscript functions via TreatAsContent:
// Safe: set value via Variable.SetValue, reference by name in AMPscript
Variable.SetValue("@inputStr", userValue);
TreatAsContent("%%[Set @encoded = URLEncode(@inputStr, 1, 1)]%%");
var encoded = Variable.GetValue("@encoded");
This pattern avoids AMPscript injection vulnerabilities.
Notes
Variable names must include the @ prefix:
Variable.SetValue("@name", "Jane"); // ✅ correct
Variable.SetValue("name", "Jane"); // ⚠️ may work but non-standard