[JavaScript] Introduction to JavaScript

5 Sep

This is the second in a planned series of introductory articles on JavaScript. It is not an introduction to programming, but rather a quick crash course on what makes the language unique.

Tl;Dr Takeaways:

  • JavaScript is a scripting language for the web, and more recently, the web server
  • JavaScript is loosely typed, with one variable able to store many kinds of data over its lifetime
  • JavaScript is prototypal, not classical, and handles object-oriented goals slightly differently
  • JavaScript functions are first-class citizens, making JavaScript very expressive and unusual to read for programmers new to it

What is it?

JavaScript is a multi-paradigm interpreted language that is an implementation of the ECMA-262 standard for ECMAScript, currently in its fifth iteration. Because it was designed to be used primarily as a scripting language, it contains a high level of abstraction and no direct means of managing memory.

For example, having the JavaScript engine handle all the memory management means that we can do things with variables that would otherwise be more complex:

var a; // This line creates a variable "a"

a = "this variable now contains a string";
a = 12; // This variable now contains a number
a = function thisVariableNowContainsAFunction() {};

JavaScript has some other distinct differences from classical (or class-based) languages like C++ and Java. Because JavaScript is prototype-based, it handles the object-oriented concept of inheritance differently.

Rather than instantiating a class, of which instances may or may not already exist, each object in JavaScript must inherit directly from an existing object – which becomes the prototype it is cloned from. This will be covered in detail in a later post, but here’s a quick visual example:

var q,
    Obj1 = { ... }; // Object declaration syntax
                    // pseudocoded for brevity

    q = new Obj1(); // Clones Obj1

A second major difference is the inclusion of functions as first-class citizens of the language. First-class citizens are types that are able to be assigned into a variable, passed as a parameter to a function, and returned as the return value of a function.

As a result, it is very common to see function calls like this one:

var q = someFunction( "a string", function( booyah ) {
  if ( booyah ) {
    alert( "woo!" );
  }
});

At this point, most JavaScript n00bz have a similar, and quite confused reaction. If you look closely, the second parameter of the call to the function called someFunction is another function, complete with the function syntax you’ve already seen.

Why do we use it?

Originally, JavaScript was used entirely for client-side (read, web-page) based programming in Netscape and these days it basically is programming for the web. More recently though, other technologies have provided virtualized browser environments that run in other settings.

NodeJS is an excellent example, allowing server-side programming in JavaScript. Combined with how expressive the language is, it’s quickly become a favourite in the web development scene and is becoming a valuable skill.

Conclusion

I’m going to stop here. Next up will be a discussion of types, and what an object really is in JavaScript.

Please feel free to leave comments if you have questions or any feedback!

Leave a comment