JavaScript No-Nos
Below are code snippets that illustrate some of the flexible features of JavaScript and how they can introduce unexpected/unintuitive behavior. There are many more; see Appendix B of Crockford’s JavaScript: The Good Parts. Please note that there are useful, positive side of most of these; every language design/implementation decision is a trade-off.
Automatic semicolon insertion
Eval
JavaScript’s eval function can be used load and execute new code, at runtime.
This is generally considered a bad/dangerous idea, but nevertheless useful in
some cases. Beyond security implications (mostly a concern in the browser since
eval can easily be leverage to carry out XSS
attacks), however, the
behavior of eval differs when you call it directly or indirectly. This
difference was introduced in the name of performance. (Arguably okay since you
should not be using eval very much, but not great.)
Implicit casting
See the sarcastic WAT video.
Monkeypatching
Because JavaScript lets you modify arbitrary objects and their prototypes, running untrusted code safely is extremely difficult to do. (If you’re interested in this, come talk to me; this is some of my research.) See the examples here on global object poisoning. Here is the simplest: