Our first program will demonstrate how to declare and use variables in Clojure. Here’s the full source code:
In Clojure, variables are declared and used differently compared to some other languages:
The def special form is used to create global bindings (vars).
The let special form is used for local bindings.
Clojure is dynamically typed, so type declarations are not necessary.
Uninitialized variables are nil by default.
To run the program, save it as variables_example.clj and use the Clojure CLI or REPL:
Clojure programs are typically run directly without a separate compilation step. However, you can compile Clojure code into Java bytecode for faster startup:
Now that we’ve seen how to declare and use variables in Clojure, let’s explore more features of the language.