JavaScript – It’s a Real Language!

October 1st, 2010 by Bob Byron

JavaScript doesn’t get much respect. “It’s a toy!”, they say. The language has been around ever since the earliest browsers.  But did you know JavaScript is a real language? It seems that only front end developers realize that it is!  Where is the confusion?  You have to look at the language’s origins.  Traditionally, JavaScript has always been part of the browser.   This absolutely has contributed to the popularity it enjoys today, but has also mischaracterized it as a toy for browsers.  It has simply been used as the means to get the cool things done you can’t do with HTML alone.

JavaScript - It's a Real Language!

Browsers Are So DOM

Many developers look at browsers as a single unit, but browsers have a dual personality. Part of the browser contains the Document Object Model, the DOM. The other part of the browser contains the JavaScript engine. You might be surprised at how this simple concept eludes many.  When a web page is written, the HTML is the source code for the DOM and the DOM is rendered to the screen.  How that DOM is rendered is where we end up getting into browser compatibility issues.  By using a developer utility like Firebug, select the HTML tab to see how the HTML is translated into a tree-like structure or a more exact representation can be found on the DOM tab.  Further, by changing those values manually we can see how the DOM effects what has been rendered on the screen.  Make no mistake here, JavaScript doesn’t write to the screen, JavaScript modifies the DOM, that effects the rendering of the screen.

Architecture Influences Opinion

Part of JavaScript’s perception problem is the prominence of poorly written code.  Many websites out there could be written better.  Coders for these sites often mix HTML elements with JavaScript, leaving the site with a big muddle of spaghetti code (it’s all mixed together).  It’s no wonder JavaScript has such a bad reputation.  To overcome this, code separation needs to be encouraged.  One needs to view the browser with distinct architectures, DOM and interpreter (HTML and JavaScript, respectively).   JavaScript is more effective when it has been abstracted into its own modules – separation from the HTML is important.  You can often see an HTML element defined with event attributes executing JavaScript.  These attributes like onkeypress, onclick, and onmouseover serve to confuse the design.  Although it is often easier and quicker to get it done that way, ultimately, it makes maintaining and debugging the code more difficult and that devalues the language.  Make sure you have an architect that will separate the code as early in the development cycle as possible – it simplifies design. Once this architectural abstraction is achieved, once you start thinking this way, you might be surprised at how easy it will be for you to visualize your coding tasks.

A Real Language is an Object Oriented Language

Object oriented languages have come to form a major foundation of the programming world.  Granted, there are other important approaches to follow like aspect oriented programming, modular design, etc, but OOP is very popular.  Object oriented languages come in many shapes and sizes.  SmallTalk, Java, C++ are all great, but they have varying degrees of how object-oriented they really are.  JavaScript is no different.  Closer to the C++ end of the chart, JavaScript was designed as more of a structured language, but it can be as OO as you want it to be.  JavaScript easily supports object inheritance and you can even create private members.  Although JavaScript makes extensive use of objects, the language itself has no object oriented requirements, meaning you can still write JavaScript code in a structured fashion.  But, once you choose to write code in an OO fashion, the addition of a foundation library can be helpful.  Dojo Toolkit is a foundation library that was written with an OO approach in mind and can assist you in achieving your object oriented structure.

JavaScript is Loose and Flexible

If you have coded for many years in a language like Java or C++, it is easy to make a premature judgment of JavaScript.  It can be easy to point to JavaScript’s lack of strictness as the reason it’s a toy.  After all, JavaScript does not have to define a variable in order to use it.  Nor do you have to define the type of structure a variable is to represent.  Is this looser style of definition wrong?  Well, the answer is in the eye of the beholder!  But, it makes for some short cuts in writing code that can help you get things out the door quicker.

It might surprise you that objects in JavaScript have an interesting advantage over those in compiled languages like Java.  In JavaScript, one can take any object and add new methods directly to it that were not there on creation.  This means if you receive the JSON object myobj from a server and you want to add methods to that object, it is simply a matter of saying:

var myobj = {};
myobj.myfun = function(txt) {alert(txt);};
myobj.myfun("hello");

From then on, your object has myfun available.  This can be a very powerful feature.  Suppose you receive an object from a server, wouldn’t it be nice to test the object and extend it with a method specific method instead of testing it repeatedly every time you wanted to perform a common action? You may be able to emulate this with other languages, but JavaScript has it built in.

Built-In Objects and Functions

In addition to the language, JavaScript has objects built in to the engine.  These objects allow for a great many programmatic conveniences.  They, in essence, give JavaScript access to powerful utilities such as Math, Date, RegExp and more.  But, those are just the JavaScript Objects.  Browsers offer access to their environment including DOM objects.  JavaScript doesn’t have HTTP and HTTPS communications built into it but it can utilize the portions of the browser that do.  The regular expressions  gives the programmer access to powerful pattern matching that can be used with substitution functions.

But it’s Interpreted, not Compiled

Usually, this point comes down to a debate over performance.  Languages like Java and C++ are compiled, JavaScript is interpreted.  The compiling process includes taking the time necessary to optimize the code that is transformed into machine language.  Despite JavaScript’s interpretive nature, its performance is being improved greatly through the use of Just-in-Time compilers.  But an interpreted language has its strengths too.  JavaScript can be run immediately without the delay of compilation, giving feedback on your changes quicker.  You can create your own statements dynamically and have them evaluated on the fly.  Often the memory footprint of the application is smaller, this is achieved by deferring the load of different sections of the code until they are needed.  Compiler advocates argue that they can achieve a higher optimization of code than could be achieved through interpretation.  This argument does have merit, but not as much as it once did.  For many years, JavaScript has had tools to help reduce the size of source code.

The Server Side

JavaScript is known by most people as that language in the browser, but JavaScript also runs on a server.  It’s kind of crazy, right?  But given the movement towards having scripting languages like Ruby or PHP run on the server, doesn’t it make sense to consider JavaScript?  Even Microsoft saw the wisdom as Windows IIS had JScript built-in since 1997.  This language can work in tandem with other web servers on the system.  The language offers the flexibility to adapt quickly to new requirements while other servers can handle the more processing intensive code.  Further, it would allow allow for developers to share code between client and server as an additional bonus.  You might check out Rhino or NodeJS when you get some time.

Conclusion

JavaScript is a powerful language.  It is often mischaracterized as a toy as a result of poorly designed code that does not abstract the HTML from the JavaScript.  Better organized code that abstracts the DOM from JavaScript helps developers write a more understandable code base that is more sustainable in the long term.  JavaScript has a dynamic nature that gives it an edge over stricter languages, enabling one to extend objects methods on the fly.  The improvements in performance recently have been extensive and show the industries seriousness in supporting JavaScript.  Server side JavaScript is also making inroads as a scripting server.  All in all, JavaScript has what it takes to accomplish the tasks you have today.

Tags: , , , , , ,

15 Responses to “JavaScript – It’s a Real Language!”

  1. tc.ca says:

    affects the rendering of the screen

  2. Andrew says:

    IBM WebSphere.

  3. Matej says:

    “Languages like Java and C++ are compiled, JavaScript is interpreted”.

    Well, actually… Java is also an interpreted language.

  4. Google Apps Script, a cloud-based scripting platform, with easy access to Google Apps. It uses JavaScript as a server-side language.

  5. MB says:

    If you’d like to leverage in JavaScript some of the OO concepts and techniques you’ll find in languages like Ruby and Smalltalk, make sure to check out Joose:

    http://joose.it/
    http://openjsan.org/doc/s/sa/samuraijack/Joose/3.011/lib/Joose.html

    Joose formalizes an “object system” on top of JS, programmed in JS, which allows for for classes, roles, modules, etc.

    Joose works in browsers, with node.js, and it’s quite possible to build cross-platform libraries which work without modification in both kinds of environments.

  6. Donald Smith says:

    Matej says:
    October 1, 2010 at 11:37 am
    “Languages like Java and C++ are compiled, JavaScript is interpreted”.

    Well, actually… Java is also an interpreted language.

    Although you are correct, the intention is really that Java has strict rules at compile time. JavaScript does not. Mainly, you have a compiler.

  7. Mike says:

    > Well, actually… Java is also an interpreted language.

    Java by itself is not interpreted, Java will be compiled into bytecode. Some parts of this bytecode will be interpreted some will be compiled again in native code.

    Compiling Java into bytecode is, for the programmer, almost the same as compiling C++.
    Java is much more on the “compiler side” than on the interpreter side.

    I think Javscript already missed an evolutionary step. For me that step was ActionScript.
    The flexibility of JS is great for small Browser-App’s but a horror for big Business-Apps – but thats just my humble opinion.

  8. JavaScript is compiled. There’s no JavaScript implementation I’ve heard of that interprets source code directly. It would be very difficult to do so, considering features like the ability to call a function before its definition. Every JavaScript implementation in wide use compiles the JS code to some intermediate form such as bytecode – just like Java.

  9. Danijel Pančić says:

    About OOP in JavaScript… you can hardly get any more object oriented than JavaScript (if that is even possible). And let’s not get into the beauty of function being a first-class citizen in JS. I think that all JS needs is it’s own proper server-side MVC framework. One that provides connections to a SQL DB, so CouchDB doesn’t count :)

  10. Ian says:

    Can you do byte manipulation with JavaScript? It was my impression you could not, so say for example you wanted to write a compression algorithm, or hashing, or encryption in JavaScript. Seems like something that could easily be added, or maybe it is already there and I overlooked it.

  11. Mike Wilcox says:

    You can encode and decode strings into byteArrays with bitwise operators. The modern browsers have access to File and Bitmap objects where you have even more control.

    Dojo has all of the above: compression algorithms, hashing, and encryption.

  12. [...] tool for developing web applications. At one point, I would probably have agreed with you, but javascript is a real language – really! The productivity increase from using jQuery properly and intelligently is [...]

  13. [...] JavaScript – It’s a Real Language! (clubajax.org) [...]

  14. [...] video is not intended to answer any pragmatic questions. To see why JavaScript is not a toy, see Bob’s post. This video is focused on trying to have that conversation. Help us spread the [...]

  15. Charles says:

    “Tomahto.”