← Back to Snippets
SSJS

SSJS And AMPScript Variables

Get variables from SSJS into AMPScript and from AMPScript into SSJS.


JavaScript
%%[
    /* 1. Define variables in Ampscript */
    SET @EmailAddress = "test@example.com"
    SET @SubscriberKey = "SK_12345"
]%%

<script runat="server">
    Platform.Load("Core", "1");

    // 2. Pull Ampscript INTO SSJS
    // Use Variable.GetValue to read @variables
    var email = Variable.GetValue("@EmailAddress");
    var subKey = Variable.GetValue("@SubscriberKey");

    // ... Perform complex logic, JSON parsing, or API calls here ...
    var status = "Active";
    var userScore = 100;

    // 3. Push SSJS back OUT to Ampscript
    // Use Variable.SetValue to write to @variables
    Variable.SetValue("@UserStatus", status);
    Variable.SetValue("@UserScore", userScore);
</script>

<!-- 4. Output the result using Ampscript -->
<p>User Status: %%=v(@UserStatus)=%%</p>
<p>Score: %%=v(@UserScore)=%%</p>