Premium coming soon
← Back to Blog

JavaScript-Defined Custom Variables: Script Your Snippets

When we launched Python-defined custom variables, the response was clear: programmable snippets are powerful. Now we are adding a second language option: JavaScript-defined custom variables. If JavaScript is your language of choice - or if you already have Node.js installed - you can now write custom variable functions in JavaScript without needing Python on your system.

Why JavaScript?

Python was a natural first choice for custom variables, but not everyone has Python installed. Node.js, on the other hand, is one of the most widely installed runtimes - especially among developers and power users. Adding JavaScript support means more users can take advantage of custom variables without installing additional tooling.

Both Python and JavaScript custom variables work the same way in your snippets. You pick the language when you create the variable, write your function, and use {{your_variable}} in your snippet body. Snip-Its handles the rest.

How It Works

Each JavaScript custom variable is a function. You give it a name, write the function body, and Snip-Its calls it at expansion time. The rules mirror the Python version:

  • Define a function with the exact variable name: function hello() {}
  • The function must take no arguments (parentheses must be empty)
  • It must contain at least one return statement
  • The return value must be a string or convertible to string
  • The function is called once per expansion - keep it fast
  • Network requests are allowed but subject to a 5-second timeout

Here is the basic template:

function hello() {
    // Your code here
    return "Hello from hello";
}

Then use {{hello}} anywhere in your snippet body - exactly the same as a Python variable.

Example: Fiscal Quarter

The same fiscal quarter variable from the Python post, now in JavaScript:

function fiscal_quarter() {
    const now = new Date();
    const quarter = Math.ceil((now.getMonth() + 1) / 3);
    return `Q${quarter} FY${now.getFullYear()}`;
}

Use {{fiscal_quarter}} in your snippet, and it expands to something like Q1 FY2026 every time.

Example: Random Meeting ID

Generate a unique meeting ID for calendar invites:

function meeting_id() {
    const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    let code = "";
    for (let i = 0; i < 8; i++) {
        code += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return `MTG-${code}`;
}

Each expansion produces a fresh ID like MTG-K7X2BN4P.

Example: Days Until Friday

A countdown you can drop into any message:

function days_until_friday() {
    const today = new Date();
    let daysAhead = 5 - today.getDay(); // Friday = 5
    if (daysAhead <= 0) daysAhead += 7;
    return `${daysAhead} days until Friday`;
}

Example: ISO Timestamp

JavaScript's built-in Date object makes timestamps trivial:

function iso_timestamp() {
    return new Date().toISOString();
}

Expands to something like 2026-02-21T14:30:00.000Z.

Python or JavaScript - Your Choice

You can mix and match. Some variables can be Python, others JavaScript - Snip-Its tracks the language per variable. Use whichever language fits the task or your preference:

  • Python - great for data processing, math, CSV parsing, and environments where Python is already installed
  • JavaScript - great for string manipulation, JSON handling, date formatting, and environments where Node.js is available

Both languages have the same 5-second execution timeout, the same rules for function signatures, and the same {{variable_name}} syntax in your snippets.

What You Can Build

Because custom variables run real JavaScript via Node.js, the possibilities are broad:

  • Formatted timestamps in any locale or format using Intl.DateTimeFormat
  • Calculated values like invoice totals, unit conversions, or countdowns
  • JSON generation for API payloads, config files, or test fixtures
  • Template generation for emails, reports, or code scaffolding
  • Random data for test fixtures, placeholder content, or IDs
  • Network requests using the built-in fetch API (Node 18+) or https module (within 5s timeout)

Reserved Variable Names

The following names are used by built-in variables and cannot be used for custom variables: date, time, datetime, clipboard, username, hostname, os, cursor, random, input, snippet.

Requirements

JavaScript custom variables require:

  • Node.js 16 or later installed on your system
  • Snip-Its 1.0.0 or later

By default, Snip-Its uses the Node.js interpreter found on your system PATH. You can also point Snip-Its to a specific Node.js binary in Settings. Execution timeout is 5 seconds per variable.

Security and Privacy

Just like Python variables, JavaScript custom variable scripts run locally on your machine. Your scripts are stored alongside your snip-its in local storage - they are never sent to any server. You have full control over what your scripts do.

Get Started

JavaScript-defined custom variables are available now for all users. Download the latest version and start building dynamic snip-its.

Already have Snip-Its installed? The app will prompt you to update automatically. Once updated, head to the snippet editor, add a custom variable, choose JavaScript as the language, write your function, and use {{your_variable}} in your snippet.

Custom Variables, Now Free

Python and JavaScript custom variables are included in the free tier. Need more than 5 snip-its? Premium is $2/month billed annually.