JavaScript - The language of the web and beyond

Florian Rappl, Departement of Theoretical Physics, University of Regensburg

JavaScript - The language of the web and beyond


  • Call it ECMAScript
  • Basic syntax
  • JS for shell scripting
  • In the browser
  • Example

A short history

  • Originally named LiveScript by Brendan Eich (Netscape)
  • Standardization finished in June 1997 (First Edition)
  • Current standard version is 5.1 (JUne 2011)
  • Most browsers support only up to version 3
  • Many different dialects exist (but do not worry about them!)

What is possible?

  • 2D (e.g. <canvas />) and 3D graphics (e.g. WebGL)
  • Real time TCP/IP communication (e.g. WebSockets)
  • Multi-Threading (e.g. WebWorker)
  • Capturing hardware input like webcam, etc.
  • Compiling anything for the web (e.g. flash without any plugin)
  • Emulating hardware (NES, PC (!), C64, ...)

A quick demo - running Linux

One of many examples

ECMAScript syntax

  • It's a dynamic language (everything's var)
  • Scope is not given by curly brackets, but functions
  • Functions are also just objects
  • Standard logical operators perform casts automatically
  • New logic operators like === do not cast

Less rules more vision

  • Besides it's in a way C-style - consider
    var a = 2; // Line-comment
    var b = 25.3; /* Block-comment */
    var c = a + b - a / b + a * a - b * b;
    var d = "I am a string!";
    var e = 'I am also a string!';
    var f = d + a; // Works, string + obj = string
  • Everything is an object
  • There are only a few types built-in


  • ECMAScript uses 32-bit for numbers AND
    var a = 10, b = 0, c = -0;
    if(a/b !== a/c)
    	alert('How is it possible?');
  • Always prefer the literal use, like
    "abc"; //Better than new String("abc");
    [1, 2, 3]; //Better than new Array(1, 2, 3);
    { name: 'Florian', age: 28 }; //Better than new Object();
    /[a-zA-Z]/g; //Better than new RegExp("a-zA-Z", "g");

Want it for shell-scripting?!

  • Easily possible with many (or your own?) implementation
  • Most popular choice might be Node.js
  • Node.js has been developed to write server-side JavaScript
  • Code is compiled using Google's V8 (very fast - but still dynamic)
  • Download the tarball from
  • Installing npm yields a powerful package manager

How to set up Node.js

  • Unpack and build it like
    tar -zxf node-v0.8.15.tar.gz
    cd node-v0.8.15
    ./configure # --prefix=/opt/node
    sudo make install
  • Maybe add export PATH=$PATH:/opt/node/bin to ~/.profile

A short sample script

#!/usr/bin/env node
var fs = require('fs'); //include package
if(process.argv.length < 3) { //0 = node, 1 = script, 2 = arg #1
	console.log("You have to specify at least 1 argument.");
} else {
	var fileName = process.argv[2];
	var text = "[ " + process.argv.slice(3).join(' ; ') + " ]";
	fs.writeFileSync(fileName, "Args:\n" + text, 'utf8');
	console.log("The file " + fileName + " has been created.");

A few remarks

  • Use chmod u+x $fn to execute directly
  • Writing to stdout by using console.log()
  • Reading from stdin with the process.stdin object
  • Execute a shell command via child_process.exec()
  • Recommended is require('path') if you use paths
  • Integration of C++ programs is possible - read more

JavaScript and Chroma!

  • Chroma generates XML files (input and output)
  • The browser is pretty good at XML
  • Browsers do have JavaScript and JS consoles included
  • Much easier to inspect than with grep etc.
  • Simple example: Sum of all w_plaq nodes

Up to the browser

  • All good browsers have a JS console
  • JavaScript is required for every web app
  • The objects we get from the browser is remarkable
  • Possibilities: Requests, graphics, evaluation, ...
  • Manipulate open websites (ofc. just locally!)

The DOM (not "der Dom"...)

  • Basic object is window
  • This object contains a property document
  • Includes the nodes
  • Query by getElementsByTagName(), getElementById(), ...
  • We get back HTMLElement objects
  • Can manipulate their properties
  • Simple example: change background of Google

Drawing a christmas tree!

  • We will use the browser for displaying graphics
  • We need several methods (don't worry, I prepared something ...)
  • Want it dynamic? If you got some time during Christmas
  • Let's start!
Brandan Fraser
Why the long freeze in JavaScript development? A lot of it had to do with the long freeze on browsers due to the IE takeover.

Brendan Eich