Javascript

Brief History

  • 1995 : Creation of Javascript as a scripting language.
  • 1997 : Standardization by ECMA under the name... ECMAscript
  • This gave birth of a flury of various implementations
  • 2005 : Starting point of the AJAX revolution: stirred the development of the language
    • 6 versions between 2009 and 2019
    • Now at version 10
    • classes, modules, algebraic datatypes, iterators, etc
  • Nowaday: Backbone of the web, underlying language for all existing webapps

Characteristics

  • Imperative language (program = series of commands)
  • Dynamic typing (as e.g. Python)
  • Object-oriented (therefore reference passing)
  • Functional (manipulation of functions)
  • Java/C-like syntax (end commands with semi-columns!)

Instructions and Variables

// this is a one-line comment
/* this is
 a comment on several lines */
var i = 0;     // instructions end with a semi-column
var j = i + 1; // variables are allocated with keyword "var"
var k;         // k is undefined
k = j          // now k is an integer with value 1 
{ k = 1; j = 2; 
i = 3;
} /* this is a block of instructions. 
Note how space and newlines are irrelevant */

Control Structure

// Test
if(i == 1) { // value equality with "=="
 …
} else {
 …
}
// loop with
while(i > 0) { … } 
// or
for(i = 1 ; i < 10 ; ++i) { … } 
// or
for(i in object) { … } 

Input/Output

// print on the console (with Firefox open it with Control-Shift-K)
console.log("Hello World!");
// Pop-ups
alert("Hello World"); // pop up with only "OK"
var b = confirm("Are you ok?"); // return a boolean
var s = prompt("Give me a string"); // returns a string

Arrays and Strings

/* An Array can contain anything */
var t = ["Hello", 1, true, 3.4];
t.length      // length of the Array
var x = t[0]; // first cell of t - x is the string "Hello"
var u = t;    // u and t maps to the same array in memory
u[1] = false; // changing true in false in the array

// Strings are like arrays of one-char strings
var s = "abc";
var c = s[0];  // now c is "a"
var w = s + 'def';  // string abcdef -- note how one can use " or '

// + is then both addition and concatenation
var i = "1" + 1; // what is this ? Think type casting 
// cast string into integers with parseInt
var j = parseInt("1") + 1;

Functions

Javascript is functional: functions are first-class objects.
All of these things are equivalent
function f(x) {
 return (x+1);
}         
var f = function(x) {
 return (x+1);
}
One can use f, or its definition
doSomething(f);


doSomething(function(x) {
 return (x+1);
});
The newlines are of course irrelevant
doSomething(function(x) { return (x+1); });

Functions

Javascript is functional: functions are first-class objects.
All of these things are equivalent
var f = x => {
 return (x+1);
}         
var f = x => x+1


One can use f, or its definition
doSomething(f);


doSomething(x => x+1)


The newlines are of course irrelevant
doSomething(function(x) { return (x+1); });

Objects

var point = {
 x : 1,
 y : 2,
 translate : function(a,b) { this.x += a; this.y += b }
};
Try in the console, in sequence
point;
point.x;
point.translate(3,3);
point;
point.translate(-1,3);

Error Management

  • About none
  • Always try to proceed! Try
    (function(x) { return x; }) + 1
  • Fail silently: no complain for buggy script
  • Many proposals for static types!