-Brendan Eich (creator of JavaScript)
Javascript has been here for ages. Working on the background elevating our browser experience, since the time of memorial... and now suddenly it has been pushed to the central stage? What is going on? And why should we care?
A short answer to the first of the question; "why now?" that I can think of are Ajax and the Dead of Flash (yes, it's true, Adobe told me so!). Recently web has become more mobile and web design more light-weigh & responsive. Javascript and HTML5 were the ones taking over the challenge and quite successfully at that.
Once the first question has been answered, the second should become obvious; Javascript and HTML5 are here to rule the web front-end, before we find something better, and we as developers must lear the ways of our new masters.
In terms of Javascript, I would like to focus only couple of it's treats: Closures and Dynamic Typing, mainly because neither one of these are available currently in Java ;(
Closures in Javascript.
What are closures and how do they work?
Closures are a functional programming concept. In the language where they are available, they can be used drastically simplify complex operations. However everything that can be achieved with closure in Javascript, can be achieved without them. This applies for any language, which satisfy the prove of Turing completeness[1]
var outerValue = 'OuterValue';
var later;
function innerMethod() {
var innerValue = 'InnerValue';
function innerMethod() {
assert(outerValue,"OuterValue accessible.");
assert(innerValue,"InnerValue accessible.");
}
inner = innerMethod;
}
innerMethod();
inner();
When executed this will print out: OuterValue accessible.
InnerValue accessible.
Now the question, you should be asking yourself is: "How can the inner function access the InnerValue?" (OuterValue is defined in the global scope, so no surprises there).
Here's where the Closure step in to the picture; The closure, inner, had visibility to all variable to the scope at the time of the declaration.
This is what Closures are, mostly they are used in callback functions as any JQuery or NodeJs developer knows.
var element$ = jQuery("div");
element$.html("Content Loading...");
jQuery.ajax({
url: "index.html",
success: function(html){
element$.html(html);
}
});
Within the jQuery .ajax() method, the anonymous function serves as the response callback. Within this callback, there is a reference to the element$ via the closure, which then updates the content of the div. Presence of the closure makes the functionality short and simple.
Dynamic Typing in Javascript.
The blessing and a curse of JS.
Dynamically type languages usually have:
- Bad IDEs: no autocomplete, no type for a function parameter
- No method overload
- Impossible to refactor
- You don’t have to initialize variables
- You write less code
UI development often demands better productivity and high flexibility, so dynamic typing is often preferred. And here's something you cannot do in static type language (as simply). Code is adding behavior to the object instance after creation.
empty_object = new Object();
empty_object.color = function() { return 'red'}
alert('Color: ' + empty_object.color())
Summary:
Here's but a tiny flash of JavaScript's great flexibility and power of the higher-order language. In the UI layer, it's here to stay for a while, so we might as well, dive in and get familiar with it.

