ClojureScript JavaScript Interop: js/, Dot Access, goog.object, or clj->js
How to choose between js/ globals, dot-property access, goog.object, and clj->js when compiling ClojureScript with :optimizations :advanced.
20 Sept 2025, 22:41 UTC

The decision and constraints
You need to read or call a JavaScript value from ClojureScript, and you plan to compile with :optimizations :advanced. The compiler renames local JavaScript properties to shrink the bundle, so a property access that works in development can return undefined or throw after release compilation.
Useful takeaway: prefer js/ for stable globals such as js/console and js/JSON. Use dot-property access (.-foo) only when an externs file preserves the name or when you are not using advanced optimizations. Use goog.object/get for dynamic or third-party keys. Treat clj->js and js->clj as data-only boundary conversions, not general interop tools.
Assumption: this guide targets the ClojureScript 1.10.x series. Verify behavior against your pinned compiler version, because externs handling can change between minor releases.
Supported interop options
| Syntax | Best for | Behavior under :advanced | Externs required? |
|---|---|---|---|
js/console.log | Built-in globals and stable host APIs | Safe; refers to the global object directly | No |
.-config obj | Known properties on extern-declared objects | Property name may be renamed; breaks without externs | Yes |
(goog.object/get obj "config") | Dynamic keys or third-party objects | Safe at runtime, but inhibits dead-code elimination | No |
clj->js / js->clj | Data boundaries between ClojureScript and JavaScript | Copies plain data; loses functions, prototypes, metadata, and cycles | No |
Trade-offs
js/ access is the most predictable under advanced compilation because it names a property on the global object rather than a local property that the compiler can mangle. The downside is verbosity: every global must be fully qualified.
Dot access (.-name) is concise, but under :advanced the compiler may rename name to something like a. If your externs file does not declare name, the compiled code reads a property that no longer exists.
goog.object avoids externs for dynamic keys because it performs a string lookup at runtime. The trade-off is bundle size: calls are not reliably eliminated by dead-code elimination, so the helper code remains in the output.
clj->js and js->clj copy data across the boundary. They do not preserve JavaScript prototypes, functions, ClojureScript metadata, or cyclic references. Use them only when the receiving side expects plain JSON-shaped data.
Implementation example
Create a namespace that exercises each style:
;; src/example/interop.cljs
(ns example.interop
(:require [goog.object :as gobj]))
(defn log-global [message]
(js/console.log message))
(defn parse-json [text]
(js/JSON.parse text))
(defn get-dynamic [object key]
(gobj/get object key))
(defn read-config [object]
;; Safe under :advanced only if "config" is declared in externs.js
(.-config object))
Add an externs file so the compiler preserves config:
;; externs.js
var example = {};
example.prototype = {};
example.prototype.config = null;
Configure the build for advanced compilation:
;; build.edn
{:optimizations :advanced
:output-to "target/main.js"
:externs ["externs.js"]
:main example.interop}
Validation
Run from the project root in a terminal. No special permissions are required.
- Compile with advanced optimizations:
clojure -M:cljs build build.edn
Replace build.edn with your actual build alias or path. A successful run exits with code 0 and emits target/main.js.
- Run the emitted JavaScript in Node.js or a headless browser:
node target/main.js
Expected check: no TypeError: Cannot read property for extern-declared names, and js/console or js/JSON calls resolve to the host global.
- Inspect the emitted file or source map:
Confirm that js/ references remain intact, while non-extern local properties appear renamed. If config appears as a short identifier such as a and your code still reads .-config, the externs file is missing or misdeclared.
Risk: missing externs produce silent undefined values or runtime errors that only appear after advanced compilation, not during Figwheel or REPL development.
Limitations
goog.objectcalls are not reliably eliminated by dead-code elimination under:advanced, increasing bundle size compared with direct property access.clj->jsandjs->cljdo not preserve prototypes, functions, metadata, or cyclic references.- Interop behavior is stable across ClojureScript 1.10.x releases, but compiler flags and externs handling can change in minor versions; verify against your pinned version.
Practical check
After compiling with :optimizations :advanced, search the emitted JavaScript for your extern-declared property name. If the name survives exactly as declared and the program runs without property-access errors, the interop boundary is correctly configured.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.