Skip to main content

jQuery: When does a library become a language?

jQuery is a great JavaScript library which allows web developers to write cross platform code, but some of its syntax looks like it is based more on Ruby than JavaScript. When does a library become a language?

So first of all, what is the difference betwen a programming library and a programming language?

A programming language is a means by which we (as humans) can give instructions to a computer to get it to do various tasks (algorithms, computations, etc...). They are largely artificial constructs created for human readability which then need to be compiled or interpreted into actual instructions which the computer can then carry out. A compiler turns a program into machine readable instructions ahead of time (compile time) where as an interpreter does so on the fly (runtime). Languages such as C and Java are generally compiled and JavaScript, Perl and Ruby are generally interpreted. While there is no real reason why this has to be the case, these factors normally affect the syntax of the languages in question.

A library is a collection of functions and methods of (normally) common actions which are not expressed in the original language. These are not included in the main language because not everyone will need to use them all the time. They are also written independently of the main language compiler/interpreter and they also encourage code re-use (so not everyone needs to write their own String.trim() function for example).

Because of the fact that there are many JavaScript interpreters out in the internet and JavaScript developers cannot control which one will be used to interpret their code, a few years ago some people released their own framework/libraries which iron out these differences and allow developers to write one set of unified code from which any browser differences could be abstracted out into the library.

The most popular of these are Prototype and jQuery.

jQuery primarily gives developers a generic way to access and manipulate DOM elements (one of the main differences between browsers).

However, the syntax used looks very different from plain old JavaScript.

Here is a typical JavaScript loop.

var elements = getElementsByTagName("div");
for (var i = 0; i < elements.length; i++){

Here is the same loop in jQuery

var elements = $("div");

Do these look remotely alike?

How about this?
var element = document.getElementById("myDiv");

var element = $("#myDiv");

To the untrained eye, one might think these are different languages.

jQuery is not another language though, not only does it use the same JavaScript interpreter, not only is it useless without JavaScript (i.e. it is not independent), but in jQuery you are really creating and manipulating a jQuery object and you are given the option to access the JavaScript object it references using the get() method.

It is possible though to write a language based on another language. The Ruby runtime is essentially built on the C++ framework. JavaScript runs in browsers written in C++ as well. These languages have no direct relation to C++ in their syntax though. In theory you could probably write a C++ compiler in Ruby should you want to (though that would be pointless and slow). You can write programs which translate programs from one language to another (the Google Web Toolkit translates Java into JavaScript).

I would argue that jQuery is a dialect of JavaScript or else even a kind of JavaScript slang. It differs from the main language, but it doesn't veer far enough to be completely independent or incomprehensible.


Joao said…
Hello there!

Ruby is actually based on C.

JavaScript is more usually based on C++ and I really like what the Google guys have created in their v8 JavaScript engine.

jQuery is impressive for the author really kept focused on reusing the approach for all of its tasks, but when I tried to read its code I found it quite tough, just as tough as when trying to read people's jQuery plugins, each saving data differently and trying too much to keep the conciseness. ;-)

Prototype and all other libraries work quite differently to jQuery and it seems as if the development of Prototype has stalled as of late even though it also means that it does not introduce changes that could break people's code. jQuery's popularity is very impressive and deserved!

Tools such as jQuery can be much more easily built using languages that allow more creative liberty. jQuery's success is a clue to why people seek much higher level languages than they usually get when using C, C++, Java and so on. And just as jQuery doesn't cease functioning despite all kinds of developments, just so do the tools that are built in these extremely higher level languages like Ruby and so on. Just as jQuery helps with really exercising the browsers' capabilities, tools built for instance in Ruby also really help with fixing bugs and improving the quality of the underlying platform.

There is a lot of work to make such a magic work, to make languages a success, and the roles of the libraries is to facilitate some kind of programming and not to become entire languages themselves. ;-) In Ruby, mini-languages called Domain Specific Languages of which jQuery is an example are very popular. DSLs in Ruby really push the envelope and might even scare guys like the creator of Ruby, matz, who might not like everything that he sees when users allow their creativity to flourish.

Anonymous said…
It might be interesting to notice that the original Javascript inplementation was written in Lisp.

You might then consider exploring a bit more the "lambda calculus side" of Javascript and find that, in fact, jQuery functional style isn't that "different" at all.
Douglas Webb said…
For what it's worth, on the "compiled vs interpreted" comparison, Perl and Java are pretty much the same. They both compile their source code to byte code, then run the byte code on a virtual machine. They both inherit this approach from Pascal.

The difference is that Perl's compiler is much faster; so fast that it's generally not worth running it as a separate step. That's why most people don't realize it's there. The speed and compile-at-run-time behavior gives Perl programmers most of the development efficiency of an interpreted language, and also most of the run-time speed and efficiency of a compiled language.
Anonymous said…
Regarding Joao's assertion that "Ruby is actually based on C. JavaScript is more usually based on C++ ..."

I'd say Ruby is strongly influence by Perl's loose syntax and Smalltalk's object system, and JavaScript is actually closer to Scheme than C++.
Anonymous said…
@Anonymous -- When Joao said Ruby was based on C, I suspect the reference was to the Ruby interpreter/VM are built in C, at least Matz's versions (Rubinius being based on C++).
Remember one of the biggest virtues of JavaScript is being "Prototype Based" and that give you a lot of "Beautiful Power". That's impossible or very hard to implement in other languages.

(For getting more familiar with "Prototype Based" programming or enjoying more of it make sure to check out
Unknown said…
Well, creating successives layers of languages is a way well known in the functional paradigm to improve the core of a language. The capacity of javascript to handle funtions as high-level "objects" and handle code as data and viceversa let the creation of a new "language" layer (or various) on top of the core language. This approximation is in the 1992 book on lisp of paul graham for example. ops, but where is macros??? :-P

Popular posts from this blog

Freezing Gems

What is a gem and why would you want to freeze it? In Ruby, there are times when you want to access pieces of functionality that other people of written (3rd party libraries) and you normally have 2 options. You can install a plug in or install a gem. Normally the method you use is determined by which ever is made available by the author. Gems are installed on the host machine and are pretty handy when you want to run things in the command line or else across lots of projects, but their downside is that if you use a gem in a Rails project there is no automatic publishing mechanism when you deploy your site. You will need to log onto the remote host machine and install the gem manually. Plugins are specific to Rails and are similar to gems in that they are also 3rd party libraries. However they are associated with your Rails project as opposed to your machine so they will get posted to the server on a regular deploy. Freezing a gem is the process of transforming a gem into a plug in

Unit/Functional Testing RubyAMF

One of my current projects is using RubyAMF to communicate with Flash ( On the whole this is really nice because it allows you to transfer Ruby objects directly to ActionScript ones (as opposed to translating the object into XML, sending the XML and then recreating the object in ActionScript). However, Rails does not provide a built in transport mechanism for AMF, so we cannot run functional testing directly on the data call (as we could for an XML or HTML transport layer). This is a show stopper for a lot of people (Rails w/o Unit testing = a big mess of trouble when something goes wrong). We can though serve both the HTML and the AMF formats depending on the request format. This means that we can test the object instantiation logic and make sure there are no errors in the controllers (though we cannot check the actual format of the data being served). In the controller, instead of rendering AMF alone, do the following respond_to do |format|

Responsive Web Design

I wanted to go over Responsive Web Design using CSS. In the old days of web development, we had to code to common screen sizes (i.e. 800 X 600, 1024 X 768) and we patiently waited for people to upgrade their computers to have a decent amount of screen real estate so we could design things the way we really wanted. We also took on semi stretchy web layouts etc to expand and contract appropriately. Then about 2 or 3 years ago, Apple released this little device called an iPhone with a 320 X 480 resolution which took the world by storm and suddenly a lot of people were viewing your website on a tiny screen again... Anyways, as it can be difficult to design a site which looks good on 320 X 480 AND 1680 X 1050, we need to come up with some kind of solution. One way is to sniff the client and then use an appropriate stylesheet, but then you are mixing CSS with either JavaScript or server side programming and also potentially maintaining a list of appropriate clients and stylesheets. Also,