Don’t Abuse JavaScript!
Last Updated: 28-January-2010
Introduction
JavaScript has become popular due to being useful for scripting browsers, which is an acceptable use, due to the fact that it is the only commonly-acceptable portable choice for that. However, lately, it has been utilised as a host language for many other realms where there are far saner and superior alternatives.
This page aims to explain why JavaScript should not be abused for such uses.
JavaScript Shortcomings
No sprintf
JavaScript has no sprintf-like function for formatting strings.
No date-formatting routines
In addition to that, JavaScript does not have a date formatting function.
Implicit scoping - var is optional
In JavaScript, variables can spring into existence with the var
keyword being optional.
Line breaks terminate expressions
Line breaks (in addition to “;”) terminate expressions. This can cause many subtle errors.
Starting a closure is not convenient
One needs to type “(function () { function() { ... } })” to start a closure.
Foreach Loop
JavaScript’s foreach loop loops on the indices of the list instead of its values.
Swap Two Variables
JavaScript has no easy way to swap two variables.
Size of Associative Array
JavaScript's objects when used as associative arrays have no easy way to get the number of elements contained inside them.
Filter, map and forEach
Until recently, JavaScript had no built-in filter, map
and forEach
methods for arrays and they are only supported in MSIE starting from version 9.
Lack of Numeric Ranges
JavaScript lacks numeric ranges.
The ‘this’ variable behaves erratically
The ‘this’ variable behaves erratically in JavaScript and cannot easily be relied upon.
No built-in way to repeat a string
JavaScript has no built-in way to repeat the concatenation of a string or an array several times.
No ||= operator
JavaScript does not have Perl’s or Ruby’s “||=” operator.
Links
- “JavaScript: The Good Parts” Tech Talk - should be called “JavaScript: The Bad Parts”.
- “Popularity” (via chromatic’s “A Short History of the Development of JavaScript”):
Whether any existing language could be used, instead of inventing a new one, was also not something I decided. The diktat from upper engineering management was that the language must “look like Java”. That ruled out Perl, Python, and Tcl, along with Scheme. Later, in 1996, John Ousterhout came by to pitch Tk and lament the missed opportunity for Tcl.
Ok, back to JavaScript popularity. We know certain Ajax libraries are popular. Is JavaScript popular? It’s hard to say. Some Ajax developers profess (and demonstrate) love for it. Yet many curse it, including me. I still think of it as a quickie love-child of C and Self. Dr. Johnson’s words come to mind: “the part that is good is not original, and the part that is original is not good.”
- “Upcoming Changes to the JavaScript Language” - about ECMAScript 4.0, while detailing many of the current shortcomings of JavaScript.
- Jay @ Mutation Grid: “JavaScript’s scoping != Perl’s” (4-November-2010)
- jwz: “Everyday I learn something new… and stupid” - about JavaScript’s numbers and integers.
- Tweet
Hall of Shame
- Amarok 2.0 Scripting (dropping backwards compatibility with older scripts).
- GNOME Shell