Skip to content

Variables & Functions

Designers work with named things all the time. In Figma you might create a colour token called brand-purple and reuse it across every component. Change one token, everything updates. JavaScript variables work the same way — you give a value a name, then use that name everywhere instead of repeating the raw value.

A variable is a named box that holds a value. There are two keywords for creating them:

  • const — the value is set once and never changes (like a locked colour token).
  • let — the value can be updated later (like a text layer you’ll edit).
const brandColor = '#7C3AED'; // will never change
let buttonLabel = 'Sign up'; // might change to 'Creating account…'

Use const by default. Reach for let only when you know the value will need to change.

A function is a block of instructions you give a name to, so you can run those instructions whenever you need them — just like a Figma component that performs an action when triggered.

function greet() {
var message = 'Hello, ' + name + '!';
document.querySelector('#output').textContent = message;
}

Writing a function doesn’t run it. To actually run it, you call it by writing its name followed by ():

greet(); // runs the instructions inside greet

You can call a function as many times as you like. That is the whole point — write the logic once, reuse it everywhere.

The example below stores a name in a variable, defines a greet function that builds a message using that variable, then connects the function to a button click. Press the button to see it in action.

Notice how the function uses the name variable to build its message. If you changed name to 'world', every call to greet() would automatically say 'Hello, world!' — you only update the value in one place.

ConceptWhat it doesFigma parallel
constStores a value that won’t changePublished colour token
letStores a value that can be updatedEditable local override
functionBundles instructions under a nameComponent with an interaction
Calling fn()Runs the function’s instructionsTriggering a component action
What is a variable?
When should you use `const` instead of `let`?
What does calling a function do?