Home > Programming > Why JavaScript is AWESOME

Why JavaScript is AWESOME

September 3rd, 2010

JavaScript is an awesome language. Now, a few years ago, you’d be marked out as a weirdo for saying something like that. I think there’d probably be some kind of witch-burning ceremony or something, because JavaScript was this stupid language which had stupid syntax and stupid scope and was used to do stupid things by stupid people and just led to awful spaghetti code and killed kittens.

These days people are more understanding, and more and more are coming out of the JavaScript closet. It’s starting to become cool. And I don’t mean cool as in “look at this cool bouncing text” (only works in Internet Explorer 4+ and Netscape 3+); I mean cool as in “hey, check out that cool guy over there writing JavaScript, look how cool he is!”. These days John Resig is a ninja and Douglas Crockford is Chuck Norris.

Douglas Crockford with guns

If Crockford builds JSON without quotes around the labels, it's still valid JSON. - TheReincarnator

Seriously though, what’s so great about JavaScript?

Functions or lambdas

A rose by any other name would smell as sweet, and JavaScript’s functions are sweet like chocolate. I tried to explain to a friend recently just what was so great about JavaScript. What I decided was that it was the functions. JavaScript’s functions can act like lambdas, functions and methods. They can be used to create modules, namespaces, classes and objects. A language like Ruby is a toolbox with some really neat little tools that do their job really nicely. JavaScript is a leather sheath with a really really sharp knife inside. That knife can cut anything, and with it you can do anything. You can kill a bear. You can catch fish. You can whittle a piece of wood into a pony. It’s even a toothpick.

So what makes JavaScript functions so great? It’s two things I think: first-class citizenship and closures.

Functions as first-class citizens

Functions are people too you know? Well, no they’re not actually, but they are objects. They can be passed around like any other object. They can be assigned to variables, passed as arguments, and even returned. And because you can write function literals in JavaScript, you can return a function without even naming it:

function outer() {
    return function () {
        alert("I'm in ur function, accessin' ur local variablez");

This kind of thing is really powerful. I’m not going to try to give any clever examples because it’s the kind of thing you only truly get once you’ve done it yourself and suddenly the whole universe opens up in your mind.


A closure is a funny thing to get your head around. If you’re used to a static language, then it just won’t make sense. Really it’s another one of those things that you have to do until you transcend, but I’m going to have a go explaining it.

Closures are intimately related to first-class functions. One of the things you can do with a function in JavaScript is define it within another function. You can nest functions as deep as you like. Here’s an example of a nested function:

function outer() {
    var name = 'Bob';
    function inner() {

So, in this example, when outer is called, it defines name and inner as a local variable and function, respectively (i.e. they are not visible outside of outer). Then inner is called, which alerts name (‘Bob’). That’s not very exciting though – all it shows is that a function can access variables defined in an enclosing scope. But look what happens here:

function outer() {
    var name = 'Dave';
    function inner() {
    return inner;
var something = outer();

Now outer is called, and it defines name and inner like in the previous example. This time, however, inner is returned (alternatively, I could’ve just returned it as a function literal like earlier). When outer is called, its return value is assigned to something. something is now the same function as inner. But what happens when we call something? outer has returned, so when something tries to access it it’ll probably segfault or something. Well, no, it won’t. That’s where the magic happens. When inner was returned, it somehow magically “closed over” its containing scope, essentially keeping it all available even after outer returned. You can do this with arguments as well:

function outer(arg) {
    return function () {

This time the returned function has access to the argument of the outer function. That last crazy line there with all the parentheses isn’t Lisp, it’s JavaScript, believe it or not (Lisp parentheses are nested, in case you ever need to tell the difference). outer is getting called with an argument of 5, and then the return value of that call is called itself.

These two things, first-class functions and closures, are what make JavaScript functions so powerful and flexible. Again, this power needs to be felt to be believed, and the only way to feel it is to write it.

Douglas Crockford

Another thing that’s great about JavaScript is Douglas Crockford. He not only wrote the best book on JavaScript – he’s also given the best talks on the language.

Dynamic typing

I’m a big fan of dynamic typing. It adds so much flexibility, agility and speed to programming. It also makes programming much more fun. I’ve written a few posts on the subject so no need to go into more detail here.

Conceptual purity

If I went back ten years and told anyone (myself included) that one of the great things about JavaScript was its conceptual purity, I’d be laughed out of my time machine. But it’s true, there’s something really pure about the language. What can be achieved with such simple language constructs is amazing. There are just objects and arrays (and arrays are just specialised objects). There are literals for objects and arrays. There are functions, and loops. And that’s pretty much it. No classes, no modules, no iterators, no generators, no templates or generics, no macros. None of these things because they just aren’t necessary when functions are allowed to be free as nature intended. You’d think that it would feel limiting not having these features, but it’s actually liberating. It feels clean, and pure. But powerful. Like a polar bear.

Why JavaScript isn’t awesome

There are a few small things that I don’t like about JavaScript (and are generally considered a Bad ThingTM).

Implied globals

If you don’t declare a variable as local with var, you’re making it global. I don’t need to go into why global variables are a bad thing, because everyone else has.

Semicolon insertion

There was a programme on TV once and they showed all these x-rays with weird things in like toy cars and bottles and stuff, and … oh yeah, semicolon insertion. That’s where the JavaScript interpreter makes it easier for non-programmers to write JavaScript that works and makes it harder for programmers to write JavaScript that’s correct. Silly. Here’s what happens:

return {
    javascript : "fantastic"
}; //This will return an object with a single property

//the interpreter sees this as an empty return statement so inserts a semicolon:
return //this line is interpreted as "return;" - with a semicolon
    javascript : "fantastic"
}; //this is just an empty block now.

But I love it really

It’s easy to mitigate those two things with decent coding practices. Basically, always use var, always use semicolons where necessary, and always put the opening brace at the end of the previous line, not at the beginning of the next. Finally, put anything you write through JSLint. It’s like a compiler, without the necessity.

Further reading

JavaScript: The Good Parts: This is Douglas Crockford’s book on JavaScript, and it’s brilliant. It’s only 176 pages, but manages to pack a lot into those pages. By the end you’ll know how to write a recursive descent parser for parsing JSON in JavaScript (some really beautiful code).

Move over Java, I have fallen in love with JavaScript: Miško Hevery usually writes great things about testing – here he talks about JavaScript, from the perspective of a Java programmer.

Best resources to learn JavaScript: This is a useful list of resources for learning JavaScript (as suggested by its title).

Structure and Interpretation of Computer Programs: If you still don’t understand what all the fuss is with functional programming, read this book. I’m only half-way through, but it’s already taught me more about programming than any other book. It’s also probably the reason I love JavaScript so much, because deep down, JavaScript is just Scheme in C’s clothing.

Categories: Programming Tags: ,
  1. 2fuf
    September 4th, 2010 at 13:54 | #1

    When you write:

    var something = outer();

    It reminds me of Actionscript 3 (also an ECMA-262 languague) where you can do something similar. Only in AS it would be:

    var something = outer;

    So you would assign the function reference to variable something, and then call it by adding the parentheses.

  2. September 4th, 2010 at 14:08 | #2

    ActionScript has the same syntax as JavaScript (they’re essentially the same language). You can do your example in JavaScript – the difference is that in my example I wasn’t assigning outer to something, I was assigning the return value of outer to something – the return value being another function.

  3. greggT
    September 4th, 2010 at 14:51 | #3

    i don’t see the semicolon being inserted. could you, say, draw it in red or bold perhaps?

  4. September 4th, 2010 at 20:57 | #4

    There you go, does that help? I’m only showing the code that is sent to the interpreter – the comments explain how it’s interpreted.

  5. Dave
    September 6th, 2010 at 00:24 | #5

    “If you’re used to a static language [...]“

    Closures aren’t limited to dynamic languages.

  6. vinhoverde
    September 6th, 2010 at 00:34 | #6

    Any decent lanugage has first-class functions / closures. Doesn’t make it automatically an awesome language. JavaScript has too many quirks for my taste.

  7. Jakob
    September 6th, 2010 at 11:10 | #7

    Glad you found a good tool…

    That knife can cut anything, and with it you can do anything

    But beware the "Hammer Syndrome"”

  8. September 6th, 2010 at 11:17 | #8

    Absolutely, but within JavaScript it’s almost always the best tool for the job, because it’s the only tool :)

    That doesn’t mean to say that JavaScript’s always the best tool for the job – that would be crazy talk!

  9. September 6th, 2010 at 11:28 | #9

    this is a great post !)

  10. jake
    September 6th, 2010 at 21:25 | #10

    sorry. js still sucks ;)

  11. jeo
    September 9th, 2010 at 02:07 | #11

    @Dave Yep even C# has them.

  12. September 10th, 2010 at 19:55 | #12

    Definitely agree with you, JavaScript is an awesome language! Functions as closures and first-class citizen are great things, but I’d also say that the way prototypical OO works, and that every object is a hashmap, is what makes JS awesome. That prototypes simply are regular functions and objects, with an extra way to call them. That array indexing is actually property indexing, and is a dynamic membership operator.

    Everything is so.. generalized. :)

  13. December 3rd, 2010 at 06:50 | #13

    JavaScript is fairly nice for writing short scripts that add basic interactivity to a page (which is what it was originally intended for). However, when it comes to maintaining with projects with thousands of lines distributed across hundreds of files the language is completely ill equipped.

    I believe the main reason for this is the language’s lack of functional programming features available in Haskell. For example, JavaScript has no constants, pure functions, type annotations, or any of that other stuff present in Haskell.

    The declarative purity of Haskell makes it absolutely perfect for being distributed over a network, as things like dependencies, concurrency, and parallelism can be handled automatically. Where as the process of distributing JavaScript over a network is a complete mess.

    The other things that make the language undesirable like the fact that + is used for both concatenation and addition, and there is with, eval, etc, however JSLINT can at least help you avoid some of the cruft.

    no classes, no modules, no iterators, no generators, no templates or generics

    JavaScript doesn’t have classes in the traditional OOP sense, however, they are often implemented with prototypes.

    The CommonJS group is working to implement modules, however, because of the fact that the language sucks their module system is a complete mess.

    JavaScript 1.7 provides us with iterators and generators.

    Indeed there is no templates or generics, and there certainly is many other features that arise from the lack of a type system. Which I will argue is a bad thing as people tend to put type data in their documentation rather then in their code, which means that compilers are not able to utilize that data which is being written down anyways…

    no macros.

    If you don’t miss the lack of macros either you haven’t witnessed the true power of macros in the Lisp sense, or you just haven’t written that much JavaScript.

  14. December 3rd, 2010 at 09:30 | #14

    @jhuni JavaScript isn’t Haskell, and it isn’t Lisp. It’s a very clean and simple language. Yes, there are certain things you can’t do with it, but that doesn’t mean it should be discounted as a great language. I wasn’t saying that it was the language to beat all languages, just that there’s a beauty in there that a lot of people don’t see.

  15. December 5th, 2010 at 22:51 | #15

    It’s a very clean and simple language.

    The JSLINT-valid subset of JavaScript, which is free of semi-colon insertion, bad blocks, ++, –, bitwise operators, with, eval, and several other features represents a relatively clean subset of JavaScript.

    You can probably take almost any programming language and construct a clean subset of it, that says nothing of the language itself.

    Similarily, the JavaScript language taken as a whole, with all the bad features included, can hardly be considered to be clean or simple.

    there’s a beauty in there that a lot of people don’t see.

    I don’t see it. What I see a lot of verbosity, redundancy, duplication and bloat.

    The language is verbose in the sense that to be clean and efficient you need to do so much extra work like: wrapping your for in statements with a call to Object.prototype.hasOwnProperty, caching objects to reduce property access, adding the extra = sign every time you want to check for equality, etc.

    On the other hand, the duplication and bloat arise from the inability of prototypal programming to constitute reuse. In the very core of the language a lot of functionality is duplicated across the Array and String prototypes.

    that doesn’t mean it should be discounted as a great language

    What discounts it as a great language IMO is the lack of the following features

    • namespaces
    • static typing
    • constants

    The lack of these three features forces developers to program in a completely dynamically typed global namespace. This alone is enough for me to use a JavaScript code generator such as GWT for anything other then very small scripts.

  16. eggstew
    December 11th, 2010 at 11:27 | #16

    @juhni: Nice trolling, two long comments filled with your highly personal and narrowly specified preference in programming languages.

    Clearly, JavaScript was not developed with you in mind, which begs the question: why are you replying to this post? Nobody is walking over to lisp-country to piss on your great language. Because I DO hope you realize that most of what you are describing as “missing functionality” are language features?

    If not, you must at least comprehend that different languages cater to different taste, or you just haven’t written that much of anything.

  17. eggstew
    December 11th, 2010 at 11:27 | #17

    Oh, and great article!

  18. December 11th, 2010 at 11:31 | #18

    @eggstew Cheers! :)

  19. December 12th, 2010 at 07:10 | #19


    Nobody is walking over to lisp-country to piss on your great language.

    Recall that garbage collection, dynamic typing, lambdas, recursion, eval, and the if-then-else construct were all pioneered by us. So rather then walking over to our country to “piss on us” perhaps some people have been coming here to learn.

    two long comments filled with your highly personal

    I have pointed out specific technical problems. You on the other hand are the one who is the one being subjective and resorting to personal attacks by calling me a “troll.”

    Because I DO hope you realize that most of what you are describing as “missing functionality” are language features?

    Those are not missing features??

  20. May 4th, 2011 at 17:29 | #20

    @jhuni. I appreciate your objective assessment of Javascript. You were not trolling at all.

  21. Danny
    June 8th, 2013 at 04:47 | #21

    If you’re from a static language just won’t make sense

    Wat… Haskell/OCaml/F# all make heavy use of closures….. type systems really aren’t prevalent here. “If you’re coming from a C family language” would be better.

  22. Lars
    June 24th, 2013 at 11:19 | #22

    Just to offer some perspective, i would like to compare javascript to python:

    First: python has closures, function as objects and an elegant underlying model, Then: python does not have weird equality operators, automatic “fixing” of code, global variables problems Also: - python has a lot of easy to use libraries for all kinds of purposes - python has list comprehensions: e.g. [n for n in [1,3,6,7,9] if n%3 == 0] == [3,6,9] - python uses indentation instead of {}, which is much easier to read and much harder to make errors with - python has a straightforward class system, which is (at least) much easier to read then prototypes - python has straightforward namespaces in modules, classes, methods/functions, loops, etc. - straightforward ways to adapt attribute access to object attributes/properties (e.g add logging to all attribute access of a class in 3 lines of code) - has descriptors to e.g. Add static property typing to classes - has decorators to e.g. Add logging to functions in a couple of lines - has metaclasses to fundamentally adapt class behaviour - can call c libraries without too much fuss (though i havent done that) - its relatively easy to debug, for a dynamically typed language

    The only real advantage of javascript i see at this point is that it is understood by all major browers out of the box.

    Part of my point is that you cannot really declare something great without comparing it to something similar.

    (My other point is, if you haven’t, give python a try, its easy to get into and has a lot of depth, its elegance makes your code elegant, which in turn enables you to handle much more complexity in your program).

    Cheers, Lars

  23. October 17th, 2013 at 20:54 | #23

    If you want to obtain much from this paragraph then you have to apply such strategies to your won weblog.

  24. October 17th, 2013 at 23:30 | #24

    Let’s discuss some of the reasons which influence you to adopt male enhancement pills over other methods for sexual problem. Unfortunately for this reason, people never consider that an effective treatment should first have its source in nature. Some of these firms promise the moon and the stars, but turn out to be frauds.

  25. April 15th, 2014 at 10:42 | #25

    Hello There. I found your weblog the usage of msn.

    That is an extremely neatly written article. I’ll be sure to bookmark it and come back to read extra of your useful info. Thank you for the post. I will definitely comeback.

  26. July 3rd, 2014 at 22:10 | #26

    You have made some really good points there. I checked on the net for more information about the issue and found most people will go along with your views on this site.

  27. Jonatan Machado
    July 4th, 2014 at 16:27 | #27

    Great explanation!! Thank you share!

  28. July 15th, 2014 at 14:30 | #28

    Hiya! I know this is kinda off topic however , I’d figured I’d ask. Would you be interested in trading links or maybe guest authoring a blog post or vice-versa? My site covers a lot of the same subjects as yours and I believe we could greatly benefit from each other.

    If you happen to be interested feel free to shoot me an e-mail. I look forward to hearing from you! Wonderful blog by the way!

  29. July 20th, 2014 at 06:52 | #29

    This is really interesting, You are a very skilled blogger. I’ve joined your rss feed and look forward to seeking more of your magnificent post. Also, I have shared your website in my social networks!

  30. July 20th, 2014 at 10:44 | #30

    Very nice post. I simply stumbled upon your blog and wished to say that I have truly enjoyed surfing around your weblog posts. After all I’ll be subscribing to your feed and I’m hoping you write again soon!

  31. August 14th, 2014 at 08:23 | #31

    hello!,I really like your writing so so much! share we keep in touch more about your post on AOL? I require a specialist on this space to solve my problem. May be that’s you! Taking a look ahead to look you.

  32. October 8th, 2014 at 05:32 | #32

    Hello! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy reading your posts. Can you suggest any other blogs/websites/forums that deal with the same subjects? Appreciate it!

    my site; [portable Generator Shed plans](http://www.iiconline.org/ForumRetrieve.aspx?ForumID=3171&TopicID=2240560&NoTemplate=False “portable Generator Shed plans”)

  33. October 9th, 2014 at 02:18 | #33

    Yes! Finally someone writes about very small generators.

    my homepage; portable generator 6000 watts ([Petra](http://Charliesloth.com/?attachment_id=24 “Petra”))

  34. October 27th, 2014 at 11:45 | #34

    People have thgeir own meanss [dept of health and human services](https://plus.google.com/112075254929438974429/about “dept of health and human services”) dealing with migraines but some of the also known methods are choosing analgesics, drinking coffee and sleeping in a dark room while others who have an even more serious condition of migraines use prescription pills, surgery to mend nerve damage and hyperbaric oxygen therapy. Late onset does not rule out migraine (merely one third of senior headaches are because of secondary conditions) nonetheless it makes it less likely. ‘CRF1 receptor signaling pathways are going to complete stress-related alterations of colonic function and viscerosensitivity: implications for irritable bowel syndrome.

  35. November 30th, 2014 at 10:28 | #35

    Excellent write-up. I absolutely love this site. Keep it up!

  36. December 2nd, 2014 at 15:48 | #36

    After looking into a number of the blog posts on your web page, I really appreciate your way of blogging. I saved as a favorite it to my bookmark webpage list and will be checking back soon. Take a look at my web site too and let me know how you feel.

  37. December 4th, 2014 at 11:32 | #37

    Males, in particular, should not be consuming soy protein in any significant amount, let alone twice a day every day as Visalus advises. Most of them are full of sugar and processed flour.

    It helps increase immunity to aid in fighting colds.

  38. December 10th, 2014 at 10:18 | #38

    Males, in particular, should not be consuming soy protein in any significant amount, let alone twice a day every day as Visalus advises. Food being part of the physiologic needs of people should be taken with some sort of discipline. What is fascinating is that their shakes actually works and delivers some wonderful results.

  39. December 15th, 2014 at 20:01 | #39

    Diabetes is one of those diseases which are in the world for quite a long time but no proper treatment has yet been found. Keeping blood sugar at healthy levels can prevent diabetic retinopathy from getting worse. Apple cider vinegar has often been touted as a miracle remedy and it could prove true for diabetes.

  40. December 28th, 2014 at 21:44 | #40

    Now I am going to tell you few ways in which your blogs can be optimized to get better search engine rankings. You can also choose your individual payment method, or set some problems that might make it easier to be productive like a reseller. The first thing you should know is that keyword stuffing and spammy links placed all over the content is not a good thing.

  41. January 3rd, 2015 at 07:05 | #41

    Thank you for some other great article. Where else could anyone get that type of information in such a perfect means of writing?

    I have a presentation subsequent week, and I am at the look for such info.

  42. January 12th, 2015 at 19:33 | #42

    Hello, this weekend is fastidious designed for me, because this point in time i am reading this impressive educational post here at my home.

  43. January 14th, 2015 at 02:47 | #43

    Hey there I am so excited I found your website, I really found you by error, while I was researching on Yahoo for something else, Anyhow I am here now and would just like to say thanks for a tremendous post and a all round interesting blog (I also love the theme/design), I don’t have time to read it all at the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the excellent job.

  44. January 17th, 2015 at 22:54 | #44

    Thank you a lot for sharing this with all folks you actually know what you are speaking approximately! Bookmarked. Please additionally seek advice from my web site =).

    We could have a link change agreement between us

  45. January 18th, 2015 at 14:54 | #45

    1-3 months: within tgree months you are going to be able to learn popular songs such as Sweet Homee Alabama by Lynyrd Skynyrd. All the terrific blues vocalists ass well as guitarists, Jimmi Hendrix, Eric Clapton, B. Step 1: Get a coachh or mentor to teach you proper guitar playing techniques.

  46. January 21st, 2015 at 09:59 | #46

    By also being willing to speak, you are making yourself accessible to your date.

    This makes the gas an ideal temperatures to work with. The term also covers up theselection of different and best suitable controls in order to get everythingprotected.

  47. January 22nd, 2015 at 20:52 | #47

    I believe this is among the so much vital info for me. And i am happy studying your article. But wanna commentary on few general issues, The site style is ideal, the articles is actually excellent : D.

    Good process, cheers

    My web-site [Traffic Racer iphone cheat](http://credmire79.soup.io/post/485635517/Aspects-In-Traffic-Racer-iphone-cheat-Whats “Traffic Racer iphone cheat”)

  48. January 23rd, 2015 at 18:47 | #48

    You can find [wooden embroidery hoop amazon](http://www.wrapixel.com/groups/brother-cs6000i-sewing-machine-review/ “wooden embroidery hoop amazon”) designs and in many cases receive some free designs, over the projects; which you might be able to complete. The more you practice you can find your preference. If you don’t know if you wish to embroider on caps, as an example, how do you determine you should obtain a product with a cap driver rather than cap attachment.

  49. January 25th, 2015 at 22:10 | #49

    What’s up to all, it’s in fact a nice for me to pay a visit this web site, it consists of important Information.

  50. January 26th, 2015 at 17:05 | #50

    Ich meine mal ehrlich, 36 Korrekturen des BRD Grundgesetzes.

Comment pages
1 2 3 321
Comments are closed.