Emacs Lisp: If then else (Conditional)

By Xah Lee. Date: . Last updated: .

The form for “if” expression is:

(if test body)


(if test true_body false_body)

(if (< 3 2)
    (progn 8)
  (progn 7))
;; 8

;; no false clause, return nil
(if (< 3 2)
    (message "yes")) ; nil


If you do not need a “else” part, you should use the function when instead, because it is more clear. The form is:

(when test expr1 expr2 etc)

Its meaning is the same as

(if test (progn expr1 expr2 etc) nil)

(info "(elisp) Control Structures")

Lisp Basics

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Lisp Basics


Lisp Data Structure


Lisp Symbol

Lisp Misc

Working with Elisp