Elisp Variables

Table of Contents

1. Global Variables

We can set (multiple) variables with setq, and it returns the last value. Latter expressions can contain earlier variables.

  (setq var1 value1
        var2 value2
        ...)

  ;; e.g.
  (setq xa 1
        xb (+ xa 3)) ; => 4

Moreover, we can use defvar to declare variables. It returns the symbol name. Since we can have docstrings when using defvar, this is mostly used when writing packages.

  (defvar name &optional INITVALUE DOCSTRING)

  ;; e.g.
  (defvar xx 4 "DOCSTRING")

2. Local Variables

We can use let to define local variables that are only available within body. Local variable declaration is a list of lists, where each list contains variable name and optional value (nil by default). The whole let expression returns the body’s last expression’s value.

  ;; grammar:
  (let ((var1 value1)
        (var2 value2)
        ...)
    BODY)

  ;; e.g.
  (let ((a 3)
        (b 4))
    (+ a b)) ; => 7

let* is similar to let, except that in local variable declaration, latter expression can contain symbols defined earlier.

Date: 2026-07-21 Tue