To the memory of Nicolas Classeau, 43, Director of IUT/Marne-la-Vallée,
killed in the Bataclan terrorist attack, November 13, 2015
Series Editor
Jean-Charles Pomerol
First published 2018 in Great Britain and the United States by ISTE Ltd and John Wiley & Sons, Inc.
Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms and licenses issued by the CLA. Enquiries concerning reproduction outside these terms should be sent to the publishers at the undermentioned address:
ISTE Ltd
27-37 St George’s Road
London SW19 4EU
UK
www.iste.co.uk
John Wiley & Sons, Inc.
111 River Street
Hoboken, NJ 07030
USA
www.wiley.com
© ISTE Ltd 2018
The rights of Robert Jeansoulin to be identified as the author of this work have been asserted by him in accordance with the Copyright, Designs and Patents Act 1988.
Library of Congress Control Number: 2018937244
British Library Cataloguing-in-Publication Data
A CIP record for this book is available from the British Library
ISBN 978-1-78630-204-5
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.Table I.1. History of the versions of JavaScript
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.
This part deals with the fundamentals of JavaScript, whatever the environment in which it is hosted. Most of the time, JavaScript is associated with a browser, but it is a language by itself. We use the term “Core JavaScript” or “ECMAScript” to mean what is “pure JavaScipt”, in contrast with what is added by the environment into which JavaScript is embedded.
There is no need to be a JavaScript expert, good applications can be quickly programmed, provided that you adopt some “good practices”. Coding without good practice (aka. “anti-pattern”) may seem correct while being silently error prone, with errors difficult to spot. That is why you will find “Recommendations” paragraphs throughout the chapters, suggesting ways to encode “patterns”. ES5 and ES6 standards facilitate this type of coding.
Part 1 can be used as a manual: going directly to the section that concerns one immediate problem, or instead studying a chapter more in depth, for instance with the sensitive issues of the language (e.g. prototypes, closures). Here is a quick tour of the features of this amazing language:
– Variables: declaration, definition, types (ban every “var
”):
Here, we insist on the subtle distinction between the status of “declared” and “defined” variables, which deserves a particular attention. In this chapter, the creation of the tree structure of “variable scopes”, the implicit “hoisting” and the (dangerous) implicit declaration of global variables are presented. The ES6 version provides new declaration keywords, which can avoid many (silent) causes of error.
– Controls: booleans, tests and loops (replace “for” loops by array methods):
JavaScript looks classically procedural, when it comes to controlling the status of variables. But several operators may react surprisingly, due to their “polymorphism”: they do not react in the same way according to the type (e.g. number or string) and implicit (silent) recasting can be done. We emphasize such traps, and suggestions are provided to avoid them.
– Data: characters used as numbers, and strings and dates:
An appropriate format is required to represent quantitative (numbers) or qualitative data (names, texts, dates). Any unicode character can be used, and figures can be used within numbers or names or dates: we detail some issues related to type conversions and value comparisons, which are among the tricky points.
– Objects (restrict “new
” to built-in or APIs objects):
The construction of specific objects is required to structure linked data into meaningful information and to assign it the appropriate methods. The objects in JavaScript are treated rather differently than in most object-oriented languages. We focus on the innovations of ES6 that provide better tools to build objects in the “prototype” way.
– Arrays (get rid of loops “for
”):
To handle time series or relational data, tables and matrices are required. We detail the most useful methods, added to the JavaScript Array object since ES5 or ES6, that make it possible to avoid the loops, rewriting them in a “functional” code style.
– Functions (do program functionally, as often as possible):
Functions are “first-class objects” in JavaScript. This is probably the most important part of this language, and the key reasons for its efficiency and longevity.