I wrote my first JavaScript somewhere around 15 years ago and switched almost exclusively to ClojureScript several years ago. JavaScript as a language has changed so much that, if you drop me in any modern js codebase, it’s probably 50/50 that I could actually read it without referencing google.

JavaScript is constantly changing with new syntax, new data structures, new features, etc. And for the most part, using ClojureScript just lets me ignore the constant change and focus on solving problems.

Changes in JavaScript, Handled by ClojureScript

Pipeline operator

When the pipeline operator was first proposed, developers seemed very excited about it. I was confused at the excitement because it’s just the threader macro in ClojureScript.

Example from How to try the JavaScript Pipeline Operator Today

//js
const double = (n) => n * 2;
const increment = (n) => n + 1;

// without pipeline operator
double(increment(5)); // 12

// with pipeline operator
const result = 5 
  |> increment 
  |> double
result // 12
;;cljs
(defn double [n]
  (* n 2))
(defn increment [n]
  (+ n 1))
  
;;without threader
(double (increment 5)) ;; 12

;;with threader
(-> 5
  increment
  double)

;; also have ->>, as->, some->, and cond->

Optional Chaining Operator

The optional chaining operator cleans up all checks that your properties exist when navigating an object. In ClojureScript, we would use a map and just use get-in. And if we have an object, we use Google Closure’s goog.object.

Example from ESNext: JavaScript “Optional Chaining Operator”

//js
const message = {
  body: {
    user: {
      firstName: 'Bramus',
      lastName: 'Van Damme',
    },
  },
};

//have to make sure all properties exist
const firstName = (message.body && message.body.user &&
                   message.body.user.firstName) || 'Stranger';

//don't need all the intermediate checks
const firstName = message.body?.user?.firstName || 'Stranger';
;;cljs with a map
(def message {:body {:user {:first-name "Bramus" :last-name "Van Damme"}}})

(let [first-name (get-in message [:body :user :first-name] "Stranger")]
  ...)
;;cljs with an object
(def message #js {:body #js {:user #js {:first-name "Bramus"
                                        :last-name "Van Damme"}}})

(let [first-name (or (goog.object/getValueByKeys
                       message
                       #js ["body" "user" "first-name"])
                     "Stranger")]
  ...)

Object Manipulation

There are plenty of new ways of defining, destructuring, and working with objects. Most of these are irrelevant to ClojureScript because we use maps instead of objects and have a powerful API to work with data. The new Object.entries and Object.values are just keys and vals when working with maps. JavaScript added destructuring and default values which are just part of ClojureScript’s core language.

Generators and Async/Await

I can see why JavaScript developers are excited about Generators and Async/Await, but I’ve yet to see anything that couldn’t be implemented with core.async. It wouldn’t surprise me if ClojureScript could use these internally for performance gains, but as a ClojureScript user it’s not a new feature that unlocks new value.

Regular Expressions

Regular expressions are one exception where ClojureScript developers need to keep up with JavaScript, because ClojureScript regular expressions compile to native JavaScript regular expressions. There are several proposals for new features with JavaScript regular expressions that will be visible in ClojureScript.



In general, ClojureScript makes it so that I just don’t think much about JavaScript as a language. ClojureScript deserves a lot of praise for the stability it provides given the frequent changes of the language it’s built on. For the last several years, ClojureScript developers have been free to spend time learning a consistent API without needing to track frequent language and syntax changes.

Get access to new content

New posts are at bostonou.com. Go check it out, or you can just subscribe from here.

    Reminder: You're subscribing to bostonou.com