What is JavaScript (core language vs. Web APIs)

NOTE: this is part of a series about (re-)learning JavaScript.

What is JavaScript?
Core language vs. Web APIs

JavaScript ≠ Java

First of all, JavaScript has nothing to do with Java. Two different things. JavaScript borrowed some of Java’s syntax so that programmers would like it, and “Java” is in the name because of a really bad marketing idea.

Also–you know about them–JScript and ECMAScript are not different languages, but just other names for JavaScript, mainly used to avoid legal issues from the fact that Oracle owns the trademark on “Java”.

These two things confused me at the beginning, so I thought I’d get them out of the way.

JavaScript ≠ animations and changing link colors

An Italian comedian used to say “the answer you’re looking for is inside you… but it’s wrong”.

It took me a while to get the big picture–specially because a lot of books and guides describe JavaScript like that thing that lets you move text and images around and add effects to your links.

It’s not actually like that.

JavaScript is small

JavaScript is extremely lightweight. It is designed to work within a host environment, which must actually provide and control a lot of things that JavaScript knows nothing about.

By itself, JavaScript does what any other programming language does (minus a few things)–no animations.

In fact, it doesn’t actually even have a way to talk to the outside world. In a web app, for instance, it’s the browser that knows how to print content and open windows, and lets JavaScript use those features via a series of APIs.

The core language only includes the following:

  • language syntax (parsing rules, keywords, control flow, object literal initialization…)
  • types
  • built-in objects and functions
  • global object APIs
  • prototype-based inheritance mechanism
  • error handling mechanisms
  • strict mode

Not just for browsers

Although one might think so, JavaScript is not at all confined to the browser. The core language is actually used in a variety of platforms, and is finding more and more acceptance as time goes on.

Of course, it is found in virtually every browsers, but also servers (Node.js), NoSQL databases (CouchDB and others), software like OpenOffice, Photoshop, Adobe Acrobat, etc. etc.

APIs

So, the way this is done, JavaScript always remains the same, while the underling implementation provides an API to control the system according to what’s desired.

The host environment (browser, Node.js, etc.) adds platform-specific features to JavaScript, setting the global object and adding APIs.

In the browser (or client-side JavaScript as it’s called), the window object gets added (and functions as the global object), along with a myriad of APIs including methods to work with the DOM, localStorage, etc. (I believe this is the code that does that in Firefox: http://mxr.mozilla.org/mozilla-central/source/dom/base/). It’s not JavaScript itself that lets you access elements on a web page, create content on the fly, open windows, etc., but the browser that extends it to let you do that. There are A LOT of APIs in the Web platform: https://platform.html5.org/–pretty daunting, actually.

Node adds objects and APIs to create servers and control HTTP, modules, etc. (I believe modules are the global object in Node).

CouchDB will expose an API to refine database results (via map/reduce functions, I believe). Other implementations will do their own thing.

So, what is JavaScript?

Having said what it itsn’t, what is JavaScript, then?

Definition

JavaScript is one of the world’s most popular programming languages.

[It] is classified as a prototype-based scripting language with dynamic typing and first-class functions. This mix of features makes it a multi-paradigm language, supporting object-orientedimperative, and functional programming styles.

http://en.wikipedia.org/wiki/JavaScript

The stuff above means:

  • dynamic/a scripting language: it’s not compiled, but passed to the underling engine as plain text, and interpreted on the fly. This allows changing objects and other aspects of the program at runtime (dynamic typing)
  • prototype-based: doesn’t use classes, but another mechanism for sharing information between objects called prototypal inheritance (or dynamic/differential inheritance)
  • functions are first-class citizens, meaning that you can pass a function to another function, and a function can return another function
  • it’s multi-paradigm, you can use different programming styles such as object-oriented, functional, or imperative

Features

Objects are generic containers that work like kind of a mix between regular object and hash tables, and can be updated and augmented at runtime. This is extremely cool and powerful.

It doesn’t have classes, relying instead on a very expressive prototypal inhertitance implementation, which allows you to write flexible code and avoid refactoring and other cons of class-based languages. Instead of writing classes as instructions for how objects work, objects inherit directly from other objects.

It implements lambda. Functions are objects and therefore first-class citizens–meaning that they can be passed as parameters to other functions, and be used pretty much everywhere.

Because it’s executed directly by the JavaScript engine, JavaScript doesn’t have a linker, and everything is thrown into the global space. This is an advanced topic, just saying that there are bad parts, too. :-)

Conclusion

JavaScript is sometimes used as an umbrella term to mean the DOM–and possibly lots of other stuff–without really looking into how the whole thing really works.

In reality, it’s very lightweight, and most of the functionality we associate with it is provided by language-agnostic APIs (like the DOM), and in the browser JavaScript just happens to be the most convenient way of using those APIs.

Now that JavaScript is used in lots of places–Node in particular–it’s important to make the distinction between core JavaScript and Web APIs.

Sources

NewsletterHot JS/webdev content!

Delivered to your inbox

One thought on “What is JavaScript (core language vs. Web APIs)

Leave a Reply

Your email address will not be published. Required fields are marked *