Chapter 2. Why JavaScript?
There are many programming languages out there. Why should you use JavaScript? This chapter looks at seven aspects that are important when you are choosing a programming language and argues that JavaScript does well overall:
- Is it freely available?
- Is it an elegant programming language?
- Is it useful in practice?
- Does it have good tools, especially good integrated development environments (IDEs)?
- Is it fast enough for what you want to do?
- Is it widely used?
- Does it have a future?
Is JavaScript Freely Available?
JavaScript is arguably the most open programming language there is: ECMA-262, its specification, is an ISO standard. That specification is closely followed by many implementations from independent parties. Some of those implementations are open source. Furthermore, the evolution of the language is handled by TC39, a committee comprising several companies, including all major browser vendors. Many of those companies are normally competitors, but they work together for the benefit of the language.
Is JavaScript Elegant?
Yes and no. I’ve written fair amounts of code in several programming languages from different paradigms. Therefore, I’m well aware that JavaScript isn’t the pinnacle of elegance. However, it is a very flexible language, has a reasonably elegant core, and enables you to use a mixture of object-oriented programming and functional programming.
Language compatibility between JavaScript engines used to be a problem, but isn’t anymore, partly thanks to the test262 suite that checks engines for conformance to the ECMAScript specification. In contrast, browser and DOM differences are still a challenge. That’s why it is normally best to rely on frameworks for hiding those differences.
Is JavaScript Useful?
The most beautiful programming language in the world is useless unless it allows you to write the program that you need.
Graphical User Interfaces
In the area of graphical user interfaces, JavaScript benefits from being part of HTML5. In this section, I use the term HTML5 for “the browser platform” (HTML, CSS, and browser JavaScript APIs). HTML5 is deployed widely and making constant progress. It is slowly becoming a complete layer for writing full-featured, cross-platform applications; similar to, say, the Java platform, it’s almost like an embedded operating system. One of HTML5’s selling points is that it lets you write cross-platform graphical user interfaces. Those are always a compromise: you give up some quality in exchange for not being limited to a single operating system. In the past, “cross-platform” meant Windows, Mac OS, or Linux. But we now have two additional interactive platforms: web and mobile. With HTML5, you can target all of these platforms via technologies such as PhoneGap, Chrome Apps, and TideSDK.
Additionally, several platforms have web apps as native apps or let you install them natively—for example, Chrome OS, Firefox OS, and Android.
Other Technologies Complementing JavaScript
There are more technologies than just HTML5 that complement JavaScript and make the language more useful:
- Libraries
- JavaScript has an abundance of libraries, which enable you to complete tasks ranging from parsing JavaScript (via Esprima) to processing and displaying PDF files (via PDF.js).
- Node.js
- The Node.js platform lets you write server-side code and shell scripts (build tools, test runners, etc.).
- JSON (JavaScript Object Notation, covered in Chapter 22)
- JSON is a data format rooted in JavaScript that has become popular for exchanging data on the Web (e.g., the results of web services).
- NoSQL databases (such as CouchDB and MongoDB)
- These databases tightly integrate JSON and JavaScript.
Does JavaScript Have Good Tools?
JavaScript is getting better build tools (e.g., Grunt) and test tools (e.g., mocha). Node.js makes it possible to run these kinds of tools via a shell (and not only in the browser). One risk in this area is fragmentation, as we are progressively getting too many of these tools.
The JavaScript IDE space is still nascent, but it’s quickly growing up. The complexity and dynamism of web development make this space a fertile ground for innovation. Two open source examples are Brackets and Light Table.
Additionally, browsers are becoming increasingly capable development environments. Chrome, in particular, has made impressive progress recently. It will be interesting to see how much more IDEs and browsers will be integrated in the future.
Is JavaScript Fast Enough?
JavaScript engines have made tremendous progress, evolving from slow interpreters to fast just-in-time compilers. They are now fast enough for most applications. Furthermore, new ideas are already in development to make JavaScript fast enough for the remaining applications:
- asm.js is a (very static) subset of JavaScript that runs fast on current engines, approximately 70% as fast as compiled C++. It can, for example, be used to implement performance-critical algorithmic parts of web applications. It has also been used to port C++-based games to the web platform.
-
ParallelJS parallelizes JavaScript code that uses the new array methods
mapPar
,filterPar
, andreducePar
(parallelizable versions of the existing array methodsmap
,filter
, andreduce
). In order for parallelization to work, callbacks must be written in a special style; the main restriction is that you can’t mutate data that hasn’t been created inside the callbacks.
Is JavaScript Widely Used?
A language that is widely used normally has two benefits. First, such a language is better documented and supported. Second, more programmers know it, which is important whenever you need to hire someone or are looking for customers for a tool based on the language.
JavaScript is widely used and reaps both of the aforementioned benefits:
- These days, documentation and support for JavaScript comes in all shapes and sizes: books, podcasts, blog posts, email newsletters, forums, and more. Chapter 33 points you toward important resources.
- JavaScript developers are in great demand, but their ranks are also constantly increasing.
Does JavaScript Have a Future?
Several things indicate that JavaScript has a bright future:
- The language is evolving steadily; ECMAScript 6 looks good.
- There is much JavaScript-related innovation (e.g., the aforementioned asm.js and ParallelJS, Microsoft’s TypeScript, etc.).
- The web platform of which JavaScript is an integral part is maturing rapidly.
- JavaScript is supported by a broad coalition of companies—no single person or company controls it.
Conclusion
Considering the preceding list of what makes a language attractive, JavaScript is doing remarkably well. It certainly is not perfect, but at the moment, it is hard to beat—and things are only getting better.
Get Speaking JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.