The State of JavaScript in 2015

shutterstock_275901386 (3)

In its twenty-year lifespan, JavaScript has undergone quite the roller-coaster ride. In the mid-90s, it faced several competitors for the title of Most Usable Programming Language; in the end, both Java Applets and Flash lost out, due in large part to security issues and the rise of mobile.

While developing in JavaScript wasn’t a fun experience for many years, due to incompatibilities between multiple versions of Netscape and Internet Explorer, things are a bit better now, thanks to Google’s work with Ajax; the company’s V8 interpreter helped make JavaScript faster and sexier.

In the last decade, competitiveness between browsers over speed has also made JavaScript better and better. JavaScript escaped the limitations of the browser to become an established server technology (as well as a scripting language for PDFs) and has grown a massive ecosystem of plugins, including ones based on jQuery.

That’s not to say that JavaScript is without its problems, which various organizations and developers have attempted to tackle in their own ways. For example, Dart has tried to overcome JavaScript’s scaling problem with its virtual machine and transpiler to JavaScript; but uptake remains a problem. In addition to Dart, Google has an open-source Closure Compiler for JavaScript that makes the latter download and run faster; it detects illegal code, as well.

Within the broader ecosystem of JavaScript-related developers, there’s also CoffeeScript and TypeScript, which have both sought to make JavaScript better; CoffeeScript by simplifying and improving the syntax, TypeScript by directly addressing the scalability issue.

Scaling Issues?

Anders Hejlsberg, the architect of Delphi, C# and TypeScript, once fielded a question about writing large programs in JavaScript.

“You can write large programs in JavaScript,” he replied. “You just can’t maintain them.”

Some languages are much better than others at managing complexity, which is typically done via namespaces and modules. Namespaces make it easier to avoid clashes with names of variables, constants, types and functions, while modules help break down a large application into smaller logical elements.

Managing complexity in JavaScript has been done in different frameworks. There are two incompatible flavors of modules in current JavaScript: CommonJS (in Node.js), and Asynchronous Module Definition (AMD) that needs Require.js.

In current JavaScript, namespaces are done by adding names to a global object; there were dropped plans to add namespaces in ECMAScript 4. (Confused by the use of names ECMAScript and JavaScript? ECMAScript is the official standard and JavaScript is an implementation of the standard. ActionScript 3, the language behind Flash is also an implementation of an earlier ECMAScript standard.)

ECMAScript 6

Known first by the name ECMAScript Harmony, then ECMAScript.next and ECMAScript2015, ECMAScript 6 is the current iteration of the standard; since the spec’s acceptance in June 2015, there’s been growing support from existing browsers. (The excellent kangax browser compatibility table can tell you what implements what; Firefox is currently rated the highest.)

The full spec is worth a read to give you an idea of its updates and features; the ultimate goal is a better language for writing complex applications and libraries. ECMAScript 6 specifies modules that are convenient and make it easier to export and import variables, constants, functions, and so on.

ECMAScript 6 improvements include better syntax for classes, along with new methods for strings and Arrays, Promises, Maps and Sets. There’s also some new functionality for Sets:

[js]

let set = new Set([‘David’, ‘Nick’, ‘Mark’]);

for (let x of set) {

console.log(x);

}

David

Nick

Mark

[/js]

Note the use of let, which is now a reserved word in strict mode. In non-strict mode (a.k.a., sloppy mode), it can still be used as a variable name, although that’s not recommended. Compared to var, let is block-scoped and not hoisted, either, so referring to it before the let statement will generate an error (as it does for const, which is the immutable let).

The for statement iterates through the Set using new syntax. (Iterators are any object that provides a next() method; the old Iterator and StopIteration keywords are now deprecated.)

Other Additions

It would take a much longer article to cover every new feature, so I’ve picked my favorites.

The first is Class. This makes JavaScript more like a traditional OO language; Java/C#/C++ developers will feel more at home with it.

There’s a Pythonish feel to generators which use the keyword yield. Call next() on the object and it will iterate:

[js]

function foo(x) {

while (true) {

x = x * 2;

yield x;

}

}

var y=foo(3);

y.next(); // 6

y.next(); // 12

[/js]

Plus there’s new support in Unicode as well as internalization for currencies, numbers, and date formats.

Conclusion

It’s good that JavaScript has picked up some features of languages such as Java, C# and Python. If you can’t wait to try a new feature now before it’s implemented, use Babel, which turns ECMAScript 6 code into ECMAScript 5 that you can run.

Image Credit: dreamerve/Shutterstock.com

Post a Comment

Your email address will not be published.