*
The moose likes Clojure and the fly likes Clojure versus JavaScript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » Clojure
Bookmark "Clojure versus JavaScript" Watch "Clojure versus JavaScript" New topic
Author

Clojure versus JavaScript

Sunderam Goplalan
Ranch Hand

Joined: Oct 10, 2011
Posts: 73
Hi John,

Coming from a Java Background, when I learnt Java Script, I liked many features that Java script offers that are not available in Java. For instance, the power comes from Java Script being a
dynamically typed language and functions are first class objects. i.e. they can be passed around seamlessly. I believe the same is true in Clojure. And JavaScript is a functional language too,
like Clojure.

Even though Clojure and Java Script may not be directly comparable as they solve different goals, would it be reasonably accurate to say Clojure SHARES most of the features of JavaScript?
If NOT, how is Clojure fundamentally different from JavaScript?

Thanks
GS


SCJP 5.0 , SCEA Java EE 5
John Stevenson
author
Greenhorn

Joined: Mar 25, 2013
Posts: 7
    
    5
Sunderam Goplalan wrote:Hi John,

Coming from a Java Background, when I learnt Java Script, I liked many features that Java script offers that are not available in Java. For instance, the power comes from Java Script being a
dynamically typed language and functions are first class objects. i.e. they can be passed around seamlessly. I believe the same is true in Clojure. And JavaScript is a functional language too,
like Clojure.

Even though Clojure and Java Script may not be directly comparable as they solve different goals, would it be reasonably accurate to say Clojure SHARES most of the features of JavaScript?
If NOT, how is Clojure fundamentally different from JavaScript?

Thanks
GS



Hello Sunderam,
JavaScript is taking over the world it seems and its a language that is useful to know for all programmers. There are common concepts in place between the two languages, although I'd suggest that the design and thinking process is quite different. There is also ClojureScript to consider. ClojureScript allows you to write Clojure where otherwise you would write client-side JavaScript. So just as JavaScript is heading into the server side with nodejs, Clojure is venturing into the client side with ClojureScript.

Probably the most obvious difference is in the data structures core to each of the languages. Clojure has sequences (immutable lists, vectors, maps and sets) and I am not aware of constructs in the JavaScript language that provide similar immutable data structures.

One common design approach in Clojure is to create a very clear data structure (often a big map) and define functions that act upon that data structure. With good design you can infer much of the concepts about an application by first looking at the data structure. If all the functions access the data structure without changing its state, then there is greater certainty as to what a function will do. In mathematical terms, given the same data in to a function, you should get the same result out. This is also true when your data structure is immutable.

For those times when you do need to change state, this can be easily managed in Clojure under the hood using software transactional memory (STM). STM is kind of like having an in-memory transactional database for your data, so that only one function can change state at any one time. Even when you do make a change, you are really creating a new data structure. This new data structure is efficient as it points back to original and only contains changes.

On a much smaller note, I personally find the Clojure syntax more elegant and concise compared to JavaScript, especially when writing non-blocking code.

Hope this gives you a few ideas, there is so much more detail to go into (you could write a book on it)

Thanks
John.
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 252
    
    5

JavaScript was originally thrown together in about ten days, according to its creator, and was intended to be a Lisp but the mandate was a syntax familiar to Java developers. So it's more fair to say JavaScript and Clojure both share a subset of the features of the Lisp family, with Clojure having many more of those features.

I would classify JavaScript only loosely as a functional language since it has mutable data and a prototype-based object system, and most JS is written in an imperative OO style. Clojure is fundamentally a functional language, with immutable data structure, no objects (although it supports polymorphism), and lazy sequences (whilst not a prerequisite for a functional language, I consider this extremely important).

As for solving different goals, I think they both play in the same space: ClojureScript lets you write Clojure for the browser, and JavaScript can be used for server-side code with Node.js. Both are becoming general purpose languages for both client and server.

JavaScript is a very loose language, with a lot of surprising behavior and a lot of dark corners - because it was created very quickly and has grown in a rather haphazard manner since. Clojure, by contrast, is pretty tightly defined and very carefully considered - and based on a consistent set of strong abstractions. Clojure is also fairly strongly typed at runtime, unlike JavaScript.

A final, fundamental difference is that Clojure, like other Lisps, is homoiconic, meaning "code is data". That allows for a very powerful macro system and the ability to easily build DSLs - Domain Specific Languages.
Sunderam Goplalan
Ranch Hand

Joined: Oct 10, 2011
Posts: 73
Thanks John and Sean for the detailed explanation. I can see the differences between the two languages with more clarity
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Clojure versus JavaScript
 
Similar Threads
Clojure Made Simple: What's Clojure good at?
Functional Language - Clojure
Closure vs. Groovy
Make what simple?
how to print asterisk