Home > Programming > Why JavaScript is AWESOME

Why JavaScript is AWESOME

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.

Closures

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() {
        alert(name);
    }
    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() {
        alert(name);
    }
    return inner;
}
var something = outer();
something();

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 () {
        alert(arg);
    }
}
outer(5)();

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. January 29th, 2015 at 21:01 | #1

    Los neuropsicólogos clínicos son profesionales especializados en la evaluación y rehabilitación de las secuelas secundarias a al daño cerebral.

  2. January 31st, 2015 at 20:02 | #2

    Hello, I enjoy reading alll of hourglass body figure your paycheck, [www.hexacyano.com](http://www.hexacyano.com/preview/?url=http://tinyurl.com/mmyftxt “www.hexacyano.com”), post. I like to write a little comment to support you.

  3. February 4th, 2015 at 12:40 | #3

    Healing incenses are: sandalwood, mugwort, star anise, saffron and acacia.

    Use Chrysoberyl to attract your creative energy and attract your creativity to you, you will find that the creative vibes will just flow to and thru you when you carry this crystal. inspiration, creativity and intuition within the mind.

  4. February 5th, 2015 at 04:28 | #4

    I don’t know whether it’s just me oor if everybody else experiencing probhlems with yourr website. It appears like some of the written textt in your posts are running off the screen. Can someone else please prpvide feedback and let me know if this is happening to them as well?This may be a issue with mmy wweb browser because I’ve had this happen previously. Thanks

  5. February 5th, 2015 at 17:08 | #5

    Right away I am going away to do my breakfast, when having my breakfast coming yet again to read other news.

  6. February 6th, 2015 at 16:37 | #6

    Right here is the right site for anybody who hopes to find out about this topic. You know a whole lot its almost hard to argue with you (not that I actually will need to…HaHa). You definitely put a brand new spin on a subject that’s been written about for decades. Excellent stuff, just wonderful!

  7. February 7th, 2015 at 11:58 | #7

    Pretty section of content. I just stumbled upon your web site and in accession capital to say that I get actually enjoyed account your weblog posts. Anyway I’ll be subscribing on your feeds and even I achievement you get admission to constantly rapidly.

  8. February 8th, 2015 at 05:04 | #8

    Very nice blog post. I definitely appreciate this website. Keep writing!

  9. February 10th, 2015 at 23:45 | #9

    Thiss design is incredible! You most certainly kno how to keep a reader amused. Between your wit and your videos, Iwas almost moved to start my own blog (well, almost…HaHa!) Great job.

    I really enjoyed what you had to say, and more than that, hoow you presented it. Tooo cool!

    My blog post :: [dentists in indianapolis](http://www.pavorealtulum.com/?option=com_k2&view=itemlist&task=user&id=3747 “dentists in indianapolis”)

  10. February 20th, 2015 at 20:13 | #10

    After I originally commented I seem to have clicked the -Notify me when new comments are added- checkbox and now every time a comment is added I recieve four emails with the exact same comment. Perhaps there is a way you are able to remove me from that service? Kudos!

  11. February 23rd, 2015 at 00:47 | #11

    Everything is very open with a very clear explanation of the issues. It was truly informative. Your website is useful.

    Thank you for sharing!

  12. February 25th, 2015 at 15:06 | #12

    Spot on with this write-up, I seriously believe this web site needs a great deal more attention. I’ll probably be back again to read more, thanks for the info!

  13. February 25th, 2015 at 22:07 | #13

    The trainer will resolve the prospective functions of a security personnel throughout an emergency scenario and the best ways to effectively finish these tasks.

  14. February 27th, 2015 at 19:58 | #14

    I feel that is one of the most important information for me. And i am satisfied studying your article. But wanna commentary on few common things, The website style is perfect, the articles is really nice : D. Good process, cheers

  15. March 2nd, 2015 at 00:48 | #15

    whoah this weblog is excellent i like reading your posts. Keep up the great work! You recognize, a lot of persons are hunting around for this info, you could aid them greatly.

  16. March 2nd, 2015 at 19:52 | #16

    These dolls are based on well known monster, in fact, most of them are said to be the offspring of famous monsters throughout history.

  17. March 4th, 2015 at 20:22 | #17

    Durarara 2 ep 8 english

  18. March 4th, 2015 at 21:30 | #18

    I used to be suggested this web site via my cousin. I’m no longer certain whether or not this put up is written by way of him as nobody else realize such particular approximately my difficulty. You’re wonderful! Thanks!

  19. March 6th, 2015 at 19:05 | #19

    You need to be a part of a contest for one of the finest blogs online. I will highly recommend this site!

  20. March 8th, 2015 at 14:51 | #20

    Asking questions are really nice thing if you are not understanding something totally, except this post gives good understanding yet.

  21. March 8th, 2015 at 17:11 | #21

    Just want to say your article is as amazing. The clearness in your post is simply cool and i can assume you are an expert on this subject. Well with your permission allow me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please keep up the gratifying work.

  22. March 10th, 2015 at 19:49 | #22

    Wow, that’s what I was searching for, what a data! present here at this website, thanks admin of this web site.

  23. March 19th, 2015 at 07:56 | #23

    Hi, of course this article is truly pleasant and I have learned lot of things from it regarding blogging. thanks.

  24. March 31st, 2015 at 05:01 | #24

    Excellent post. Keep posting such kind of info on your blog.

    Im really impressed by it. Hello there, You’ve done a great job. I will certainly digg it and for my part recommend to my friends. I’m sure they’ll be benefited from this web site.

  25. April 4th, 2015 at 10:05 | #25

    I drop a leave a response each time I appreciate a article on a site or I have something to valuable to contribute to the conversation. Usually it is triggered by the passion displayed in the article I looked at. And on this post Skilldrick Why JavaScript is AWESOME. I was excited enough to post a commenta response :-) I actually do have a few questions for you if you don’t mind. Could it be only me or do a few of the comments appear as if they are left by brain dead folks? :-P And, if you are writing at additional social sites, I would like to keep up with everything new you have to post. Would you make a list every one of all your community sites like your Facebook page, twitter feed, or linkedin profile?

  26. April 16th, 2015 at 01:33 | #26

    I’m extremely pleased to discover this page. I want to to thank you for ones time just for this wonderful read!! I definitely loved every little bit of it and I have you saved to fav to check out new things in your website.

  27. April 16th, 2015 at 07:24 | #27

    Tɦanks for a marvelous posting! I truly enjoyed reɑding it, you аre a great author.I will be sure to bookmark yoսr blοg and will come back dolwn the road. I want to encourage you to սltimately cntinue your great writing, have a nice morning!

    Here is my wweb site; [testosterone booster supplements](http://www.scoop.it/t/h-by-roland-farrell “testosterone booster supplements”)

  28. April 18th, 2015 at 10:30 | #28

    I think the admin of this site is actually working hard for his site, because here every information is quality based stuff.

  29. April 20th, 2015 at 07:09 | #29

    This article provides clear idea designed for the new people of blogging, that in fact how to do running a blog.

  30. April 22nd, 2015 at 03:50 | #30

    I was recommended this website by way of my cousin. I’m not sure whether or not this submit is written by him as nobody else recognize such designated about my difficulty. You’re amazing! Thank you!

  31. April 26th, 2015 at 16:43 | #31

    Hurrah! At last I got a blog from where I can in fact get useful facts regarding my study and knowledge.

  32. April 30th, 2015 at 04:10 | #32

    For most recent news you have to visit internet and on world-wide-web I found this web page as a best website for hottest updates.

  33. May 2nd, 2015 at 07:55 | #33

    I pay a visit day-to-day some web pages and sites to read articles, but this webpage gives feature based writing.

  34. May 3rd, 2015 at 11:19 | #34

    It’s amazing designed for me to have a web page, which is valuable designed for my experience. thanks admin

  35. May 25th, 2015 at 03:12 | #35

    Thanks for some other great article. The place else may just anybody get that type of info in such an ideal means of writing? I’ve a presentation subsequent week, and I am on the search for such info.

  36. May 28th, 2015 at 05:40 | #36

    I love reading through an article that will make people think. Also, many thanks for permitting me to comment!

  37. June 12th, 2015 at 20:52 | #37

    Give the preschool children pieces of fabric, tape or glue, and blocks of wood or foam. Online florists work in a different way in contrast to retail stores. One can choose any of them to make this day bright and blissful with the cordial memory.

  38. June 17th, 2015 at 16:19 | #38

    Thank you Message after Feedback: Customer gives feedback for the betterment of any product, so it is always advisable to display a pleasant message after receiving the feedback. It’s a no brinier, but you may encounter sites where there is no way to contact the webmaster. Make sure your contact information is prominently displayed.

  39. June 21st, 2015 at 11:57 | #39

    If you are going for finest contents like me, simply pay a visit this web page all the time because it provides feature contents, thanks

    my website – [cheap international call](http://Vyke.com/downloadfreecalling_app/ “cheap international call”)

  40. June 30th, 2015 at 21:54 | #40

    Para ello, se utilizan las Bolas Chinas bolas de Ben Wa, o sea, 2 bolas unidas por un cordel en cuyo interior llevan otra bolita.

  41. August 9th, 2015 at 04:58 | #41

    Simply want to say your article is as astonishing. The clearness to your publish is just great and that i can think you are an expert on this subject. Well with your permission let me to clutch your RSS feed to stay updated with imminent post. Thank you one million and please carry on the gratifying work.

  42. August 17th, 2015 at 09:54 | #42

    Excellent site you have here.. It’s difficult to find quality writing like yours nowadays. I really appreciate people like you! Take care!!

Comment pages
1 2 321
  1. September 6th, 2010 at 12:02 | #1
  2. October 11th, 2010 at 11:55 | #2
  3. November 9th, 2010 at 11:16 | #3
  4. April 26th, 2011 at 21:14 | #4
  5. July 22nd, 2014 at 12:27 | #5

Comments parsed as Markdown.