Two main facts constituted the motivation for writing this book:
This use ensures regular maintenance of the code and has led to permanent improvement in performance for more than 25 years. In recent years, several innovations of the JavaScript norm have convinced all browser providers to adopt them. Figure I.1., published October 2017, counts the contributions of internautsʼ coding development in several computer languages.
Besides “proprietary” restricted data, public open data (e.g. United Nations, INSEE, US Census Bureau, etc.) [ICS 16] and free access data, via private providers (e.g. shared Google drive) or non-profits (Wikipedia), are an enormous reservoir of universal information.
These two facts have led us to write this book as a JavaScript programming manual, an open data oriented manual, with insight on combining web data and displaying them. Since 2015, the large adoption of recent JavaScript norms encourages their use and greatly facilitates new coding practices, as described in this manual.
Data represent the common heritage of humanity; everyone should have usable and free fetching tools. Every citizen who is curious about data and has a taste for technology may become a “data scientist”, an eager amateur, able to study the data, texts or figures of their own focused hobby field. And also in the professional field, a student from a school of political science, or a journalist [NICAR], or the person in charge of an association will find many useful and relevant tools in this book.
The sequel of this introduction presents a history of the language for a better understanding of its evolution, a demystification of prejudices, a list of prerequisites and useful tools, and a list of the main features of JavaScript to introduce what the following parts will detail.
This historical notice about the birth and life of JavaScript demonstrates another motivation for learning this language: on the computer science scale, it is an “old” language (more 25 years old). How can we explain such longevity? What significant assets allowed such perennity?
let
”, “const
” declarations, Object.assign
method, etc. supported by all the recent browsers.Year | Name/alias | Description |
1998 | ECMAScript 2 | Editorial changes only |
1999 | ECMAScript 3/ES3 | Added regular expressions, try/catch statement |
ECMAScript 4 | (never released) | |
2009 | ECMAScript 5/ES5 | Added JSON support, Object.create |
2015 | ECMAScript 6/ES6/ECMAScript2015 | Added let, const, Object.assign, arrow syntax, template syntax, spread and rest syntax. Also: classes, modules |
2016 | ECMAScript 7 | Added exponential operator (**), Array#includes |
We can notice several very distinct periods:
JavaScript is a lively language, which adapted itself to the evolutions of the new uses of the Internet (e.g. video, social networks, targeted publicity, etc.). More and more web applications massively use JavaScript on the client-side, and even on the server-side, since the release of Node.js and V8.
The foundations of JavaScript are unchanged, but two evolutions deeply modify todayʼs coding practices:
We no longer code JavaScript in 2018 as we did before 2015. Using ES6 allows JavaScript to unleash its qualities rather than its faults:
NEW
;for
” loops;var
”.And your code will be shorter, more readable, and much, much easier to debug.
The Big Data should not be the “preserve of the big actors”: everybody with a browser, an Internet connection and some self-training, can work with Big Data. Everybody? As long as they are at least somewhat trained with a few basic notions on:
Your browser knows how to interpret JavaScript (it is the training tool) and most of the time without the need for an Internet connection:
console.log
results and variable values in comments.Besides, there are several “online tools” for helping us:
Most of the code “bins” of this book have been tried with W3Schools.
JavaScript is a scripting language, whose interpretation and execution depend on a script engine that requires a “host”, an environment providing basic objects, events and resources.
We must distinguish the “core JavaScript”, common foundations of the language, and the embedded JavaScript that includes the objects of a specific environment (e.g. “client-side JavaScript”).
Within the “web page ecosystem”, the environment is the browser (the “window” object). We may also find the “workers”, provided by the browser, but independent of the web page and the ecosystem of the Node.js modules, totally independent of any browser.
The script engine analyzes then runs the code in the hosting environment, which is named the “Global Object”. As soon as the engine starts, it executes two successive tasks:
From the programmer’s viewpoint, this means:
undefined
”, whose type is “undefined
”. Try these lines:
let x; // declaration without explicit definition (no assignment)
console.log( x );
console.log( typeof x );
=”
.The lexical analysis lists the variables and their “scope” (where they are known). The run-time defines the variables (gives them a defined value) and also determines their type: the type of value. We may name this “dynamic typing”, rather than “weak typing”. The values can be: primitive values (numbers, strings, boolean constants), evaluated expressions, references to objects, arrays, functions or regular expressions.
The instructions can be assignments, function calls and classic control structures such as loops or conditional instructions.
The objects can be “built-in” (Object
, Function
, Array
, Date
, Math
, JSON
, etc.) or provided by the “host ecosystem”, such as the DOM elements (document HTML), or built by the application.
JavaScript makes no distinction between “class” and “instance” notions: there are only objects and any object may become the “prototype” used to create new objects. We may wrap-up the definition of a JavaScript object as “a set of properties plus one prototype”.
Functions and objects behave in the same way, except that only a function can be invoked, or called. Any operation supported by an object is supported by a function: it makes it possible to build a “higher order function” and grounds the functional nature of JavaScript:
const mult = function(f,g){return function(n){return f(n)*g(n);}},
square = function(x) { return x * x; };
mult(square, square)(3); // -> 81
JavaScript is born for the web, and because of the web, JavaScript still lives 25 years later, and this is the motivation for writing this book. A small language, designed within 2 weeks by only one person, has proven to be surprisingly flexible. This flexibility is both an advantage, which allowed evolution and compliance with successive versions, and a drawback, in that it does not protect enough against programming errors, hard to detect and debug. JavaScript has been qualified as the “most misunderstood language” from one of its main “gurus”, Doug Crockford.
A tolerance that is too big, a control that is too weak, no “classes” for the objects, a syntax much too similar to the style of imperative and procedural languages; here are many deceptive pitfalls. By contrast, the initial choice of functions as “first-class objects” gives JavaScript the abilities of a functional language: a skill that is neglected by many programmers.
Recent normalization efforts provide new ways to avoid most pitfalls: we can code without var
declaration (avoiding many “hoisting” traps), without for
loops (avoiding index troubles), and almost without the "new
" operator (for a more direct use of prototypes). This leads to writing a more readable and easier-to-debug code.
The surprising book “If Hemingway wrote JavaScript” (see [CRO 14]) shows how it is possible to render unrecognizable, and in several different ways, a JavaScript code that, nevertheless, always carries out the same operation.
18.191.240.222