Control flow
At this point, we've already seen many examples of ClojureScript functions and their associated control flows, but we haven't really covered them in explicit detail. In this section, we'll look at various branching control flow special forms and macros, and we'll cover how to handle exceptions.
if and when
Like almost every programming language, ClojureScript uses if
for basic conditional logic. if
in ClojureScript is a special form rather than a function or a macro. Syntactically, if
takes a predicate, a form that is evaluated and yielded if the predicate returns true
, and an optional form that is evaluated and yielded if the predicate returns false
. If the optional form for the false
case is not supplied, it defaults to nil
:
cljs.user=> (if (= 1 1) "One equals one!" "One does not equal one :(") ;; => "One equals one!" cljs.user=> (if (= 1 2) "One equals two!") ;; implicit nil return value when false...