GEOG5870/1M: Web-based GIS A course on web-based mapping

An introduction to JavaScript

JavaScript is a programming language which is most widely used as a means of embedding executable content in web pages, allowing a web page to interact with the user, dynamically modify itself, and alter the general behaviour of the browser. Interpreters for JavaScript are incorporated into all of the well known web browsers and JavaScript can be used for a variety of purposes.

Early in its life-cycle, JavaScript developed a rather poor reputation: it was often used to add unnecessary (and usually irritating) bells and whistles to web pages. However, over time it has come to be used widely and is now regarded as the de facto future for web-based client programming, superseding both Java applets (which integrate poorly into HTML) and Adobe Flash (which is regarded as too tied to one company). The combination of HTML (content), CSS (style), and JavaScript (dynamism) give everything that is needed client-side, and the combination is sufficiently powerful that it is often used within Operating Systems for modern GUI implementation.

Much of the day-to-day work done by JavaScript is background detailing to smooth a user experience, including, for example, automatic error checking in web forms. But JavaScript also powers everything from web mail to shopping carts to online games. And of course, JavaScript is central to Google Maps.

Computing languages can be divided up in a number of ways, but one way to think of them is the division between Scripting Languages, like Javascript, VBA, and Python, and other languages, like Java, Visual Basic, and C++. Scripting languages were traditionally used inside other programs to extend their functionality (including, for example, controlling operating systems), while other languages were used to build full applications. Scripting languages were intended for short, non-critical, programs by non-experts, and therefore traded off being easy to learn against being less powerful and less rigorous (errors being simpler to find in shorter programs). Scripting languages were also generally run by the application they sat in, so they were transfered as human-readable code and interpreted rather than compiled into binary for distribution. However, the popularity of scripting languages has led to large user-communities, and, founded on this, the development of quite sizable libraries of code that cover areas traditionally dominated by non-scripting languages. Equally, other languages have picked up traits first developed within scripting languages, especially syntactic sugar used to hide complexity behind simpler commands. This has seen the blurring of the boundaries between the two.

In terms of syntax, JavaScript sits in the "C Family", broadly resembling C and Java. If you've used either of those languages (or others in the same syntax family, such as PHP) you'll recognise the line layouts, the comment characters, the uses of different sort of brackets and so on. In terms of language structure (rather than syntax) however, it is different to those two languages. A notable difference is that JavaScript is weakly typed, as opposed to the strong typing of languages like C and Java. What does this mean? In C and Java, when you declare a variable, you have to identify what sort of variable it is – an integer, a character string, a floating point number etc. In JavaScript, this is not necessary – you declare generic variables and then can largely use them as you like. This is classic scripting language stuff: it helps the beginner, but can lead to complexities for larger programs where you lose track of what's inside variables. It is also worth noting that while JavaScript is an object-oriented language (we will come to what this means later if you haven't covered it before), objects do not necessarily behave in the same manner as in other object-oriented languages.

History

Despite the similar syntax, JavaScript has no formal relationship to the Java programming language – it justed used it as inspiration. JavaScript was first introduced in a version of the Netscape Navigator web browser released in 1995, having been briefly known as LiveScript. The Netscape Navigator web browser enjoyed a dominant market position at the time (being more advanced than rival products) and JavaScript grew rapidly in popularity; the browser declined in popularity in the face of competition from Microsoft's Internet Explorer, although a later version ultimately became the original code base for Mozilla's Firefox browser.

Microsoft added a version of JavaScript called JScript to it's Internet Explorer browser in 1996, and shortly after this Netscape submitted JavaScript to Ecma International, a privately run international standards organisation. Ecma published a formal standard for a language specification in 1997, with a number of revisions since then, the most recent being in 2015.

Formally, the language specified in Ecma's standards is known as ECMAScript, with JavaScript and JScript being variant dialects of this language, although in practice everyone refers to the language(s) collectively as JavaScript.

An implication of this history – and the important aspect to note – is that the versions of JavaScript (or 'ECMAScript' if we wish to be pedantic) in different browsers are different. If you do extensive programming in JavaScript, you'll find that you come up against these language incompatibilities, although in the work done in this module they should not be a problem.



[ Next: Language Elements ]
[
Course Index]