Assignment
Statements
  • Javascript is case sensitive
  • /* Block Comment
     */
    var a = "Hello World!"; // Line Comment
    document.write(a+"<br>");
    
    var x = 1, y = 2;
    document.write(x+y)
    		
    Arithmetic
    var a = 10, b = 100;
    
    document.write("a + b = " + (a+b) + "<br>"); // +
    document.write("a - b = " + (a-b) + "<br>"); // -
    document.write("a * b = " + (a*b) + "<br>"); // *
    document.write("a / b = " + (a/b) + "<br>"); // /
    document.write("a ** 2 = " + (a**2) + "<br>"); // **, exponentiation
    document.write("a % 3 = " + (a%b) + "<br>"); // / %, modulus
    document.write("++a = " + (++a) + "<br>"); // ++
    document.write("--a = " + (--a) + "<br>"); // --
    
    a += 1;
    a -= 1;
    a *= 2;
    a /= 2;
    		
    Assignment for Primitive and Object
    // primitive type, immutable
    var a = 10;
    var b = a; // create a copy of a, a and b point to the same object
    
    console.log("a: "+a+" b: "+b+"<br>"); // a: 10 b: 10
    
    b = 100; // create a new object, let b point to the created object
    console.log("a: "+a+" b: "+b+"<br>"); // a: 10 b: 100
    
    // object, mutable
    var car = {color: 'blue', maker: 'Honda'};
    var car2 = car; // car and car2 point to the same object
    
    console.log(car); // {color: "blue", maker: "Honda"}
    console.log(car2); // {color: "blue", maker: "Honda"}
    
    car2.color = 'red';
    
    console.log(car); // {color: "red", maker: "Honda"}
    console.log(car2); // {color: "red", maker: "Honda"}
    		
    Hoisting
  • A variable can be used before it has been declared
  • x = 10;
    
    console.log(x);
    
    var x;
    		
    Strict Mode
  • With strict mode, can not use undeclared variables
  • Strict mode is declared by adding "use strict"; to the beginning of a script or a function
  • Declared at the beginning of a script, it has global scope
  • "use strict";
    x = 3.14;
    		
    "use strict";
    var obj = {};
    Object.defineProperty(obj, "x", {value:0, writable:false});
    
    console.log(obj);
    
    //obj.x = 3.14;   // This will cause an error