Hello world

strict mode:

"use strict";
  • case sensitive

  • numbers (all numbers are 64-bit floating type)

  • strings ('' and ""), there is no character type

  • Like python, strings are immutable

  • strings uses utf-16, like c#

  • array

  • boolean, true and false

  • null and undefined

  • string interpolation

    `hello ${someVariable}`
    
  • semicolon is optional. Better to always add it

  • garbage collector

  • == supports type conversion

  • === does not support type conversion. (Recommended to use this one)

console.log('hello world')
console.log(eval('3 + 5'))

To write multi-line javascript, use shift + Enter for a new line.

(function(){
  "use strict";
  /* Start of your code */
  function greetMe(yourName) {
    alert('Hello ' + yourName);
  }

  greetMe('World');
  /* End of your code */
})();

It is case sensitive. Statements are separated by ;. Comments are the same as in C/C++.

variables

./code/hello_world/variables.js
 1// use const to define a constant
 2const pizza = true;
 3console.log(pizza); // true
 4
 5var foo = 'foo';
 6if (foo) {
 7  var foo = 'fooz'
 8  console.log('foo is', foo); // foo is fooz
 9}
10
11// note it outputs foo is fooz below since
12// we use `var` inside the `if` statement
13console.log('foo is', foo); // foo is fooz
14
15var bar = 'bar';
16
17if (bar) {
18  let bar = 'barz'
19  console.log('bar is', bar); // foo is barz
20}
21
22// note it outputs bar is bar below since
23// we use `let` inside the `if` statement
24console.log('bar is', bar); // foo is bar
25
26// we should use let as much as possible
27// variables defined by `let` is scoped inside a block {}
28
29array = [];
30for (var i = 0; i < 3; i++) {
31
32  array.push(function() { console.log(i); });
33}
34array[0](); // 3
35array[1](); // 3
36array[2](); // 3
37// since we are using var in the for loop, the closures capture the same i
38
39array2 = [];
40for (let k = 0; k < 3; k++) {
41
42  array2.push(function() { console.log(k); });
43}
44array2[0](); // 0
45array2[1](); // 1
46array2[2](); // 2

array

./code/hello_world/array.js
 1let a = [ 1, 2, 3 ];
 2function sum(arr) {
 3  let s = 0;
 4  for (let x of arr) {
 5    s += x;
 6  }
 7  return s;
 8}
 9// Sum of the array [1,2,3] is 6
10console.log('Sum of the array [' + a + '] is ' + sum(a));
11
12function sum2(arr) {
13  let s = 0;
14  for (let i = 0; i != arr.length; ++i) {
15    s += arr[i];
16  }
17  return s;
18}
19console.log(sum2(a)); // 6

Note that there are two ways to iterate an array:

  • for(let x of array)

  • for(let i = 0; i != array.length; ++i) { ... }

To run the above code, use:

node array.js

class

./code/hello_world/class.js
 1class Point {
 2  constructor(x, y) {
 3    this.x = x;
 4    this.y = y;
 5  }
 6
 7  distance() { return Math.sqrt(this.x * this.x + this.y * this.y); }
 8}
 9
10let p = new Point(1, 1);
11console.log(p.distance()); // 1.4142135623730951

It defines a Point class with two fields x, y. Point has two methods: a constructor and a method distance().

Note that class names are by convention capitalized.

template strings

./code/hello_world/template_strings.js
 1let a = "a";
 2let b = "b";
 3console.log("a is " + a + ", b is " + b); // a is a, b is b
 4
 5// using string interpolation
 6console.log(`a is ${a}, b is ${b}`); // a is a, b is b
 7
 8// Note that it uses `${}`.
 9// It preserves spaces and line breaks
10
11// multiline examples
12let s = `
13a is ${a}
14b is ${b}
15`
16console.log(s);

Note that it is similar to the F-string, f" ", f' ', f""" """, f''' '''` in Python.

functions

./code/hello_world/functions.js
 1// note that is supports default arguments
 2function hello(msg = "hi") { console.log(msg); }
 3// the above is a function declaration, it is hoisted to the top
 4// in other words, we can invoke it before seeing it.
 5
 6hello();        // hi
 7hello('world'); // world
 8
 9// this is a function expression.
10const hi = function(msg = 'hi') { console.log(msg); };
11hi();        // hi
12hi('world'); // world
13
14const add = function(a, b = 1) { return a + b; };
15console.log(add(1, 2)); // 3
16console.log(add(1));    // 2
17
18// arrow functions
19// there is no keyword `function` and no `return`.
20const inc = (a, b = 1) => a + b;
21console.log(inc(1, 2)); // 3
22console.log(inc(1));    // 2
23
24const dec = a => a - 1;
25console.log(dec(10)); // 9
26
27// for multiline statement, use {}
28const sub = a => {
29  let b = a - 1;
30  return b;
31};
32console.log(sub(100)); // 99

Note that it does not have keyword arguments.