O'Reilly logo

Beautiful JavaScript by Anton Kovalyov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CHAPTER ELEVEN

JavaScript Is…

Science is what we understand well enough to explain to a computer. Art is everything else we do.

Donald Knuth

I understand there are people in this world who do not like JavaScript. I’ve gotten into enough late-night battles about CoffeeScript to have heard all the vitriol. I’ve been generally unmovable about this. I think curly braces are elegant, I think semicolons are enchanting, and I think duck typing is adorable. I thought writing this chapter would be a good opportunity to share my favorite things about JavaScript.

JavaScript Is Dynamic

JavaScript takes advantage of virtual machines for just-in-time (JIT) compilation (see V8, Node.js, and Spider Monkey for some examples). JavaScript makes excellent use of closures by having variables that exist both on the global level and the functional level.

Consider this function:

function CheckForPrefix(name){
  var prefix = "Dr.";

  if (name.indexOf(prefix) == -1)
    return function AddPrefix(){
      return prefix + name;
    }
}

JavaScript’s closures give us the ability to reference variables defined in the containing function. In this instance it enables us to keep the separation of concerns while not repeating ourselves at the same time.

JavaScript has an eval function that allows us to concatenate values and evaluate them at runtime. eval makes things slower, as it adds a compilation step, so it’s to be used sparingly; however, it does allow us to create macros, which are another dynamic language feature. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required