Javascript Language
  1. Name.
    1. As noted elsewhere, JavaScript was created by Netscape.
    2. In early days, IE called its version JScript to avoid the trade name.
    3. Standard Javascript is more properly called ECMAScript, for the ECMA which publishes the standard, ECMA-262.
    4. ECMA stands for the European Computer Manufacturers Association, but it now just calls itself ECMA International. It publishes several standards for the industry.
    5. Usually, just JavaScript.
  2. Javascript is not Java.
    1. Some similarity in syntax. Pretty much everything below the surface is different.
    2. When JavaScript was named, Java was new and had been well-received. Name seems to have been chosen to borrow some of that goodwill.
  3. Javascript was created to run in the browser, but it is first a programming language, and can be run in other contexts, including on the server side.
  4. A couple of ways to just “try out” Javascript expressions.
    1. Use the console in the browser.
      1. Chrome:
        ⋮ / more tools / developer tools / console
      2. On Fox:
        tools / browser tools / web developer tools / console
      You will find a list of error messages and a prompt at which you can type Javascipt code.
    2. Use Nodejs from the command line.
    3. Each run the Javascript language, but in different contexts, and with different builtin objects.
  5. Same comments as Java.
  6. Semicolons are terminators, as in C, Java and friends, but are implied by line breaks and some punctuation, so they may often be omitted. Most JavaScript programmers seem to use more semicolons than strictly required.
  7. Displaying values.
    1. The console will print values of expressions.
    2. Values can be printed with console.log(...).
  8. Variables
    1. Declared with var or let. Constants declared with const.
      var z; let b = 22; const c = 3*b;
    2. Declared variables can be initialized, or not. Constants can be initialized but not assigned.
    3. Case sensitive.
    4. Dynamically typed.
    5. Variable names.
      1. Start with letter or _ and contain letter, digit or _ as usual.
      2. Can also start with or contain $.
    6. Scope
      1. A var variable is scoped to the containing function, or script of not in a function. Oddly, these variable may be used before their declarations; all var declarations are implicitly moved to the top of their scope. This is called hoisting.
      2. A let or const declaration scopes to the innermost enclosing curly brace block, and is not hoisted.
      3. Let and const are newer, and that's what the cool kids use. Might be a bit more efficient as well.
  9. Basic types
    1. Boolean.
    2. undefined (value of uninitialized variables).
    3. null (different from undefined).
    4. Number (integer or float, represented as a large float).
    5. BigInt (unbounded integer)
    6. String.
    7. Symbol (unique identifier).
    8. Object (which includes functions and several important built-in types, including some listed above.)
  10. Usual numeric operators, +, -, *, / and %, and update forms. Also ++ and --.
  11. Conversions
    1. Since types are dynamic, no conversion on assignment.
    2. Using + to combine a string and number converts the number to string.
    3. Other numerical operators convert the string to a number, and give NaN if impossible.
    4. Undefined, null, 0, NaN and "" convert to false. Everything else converts to true. (Including "0" and empty arrays.)
  12. Comparison operators.
    1. Usual: ==, !=, <, >, <=, etc.
    2. Comparisons will try to convert args to the same type before comparison, so "42" == 42 is true.
    3. === for identical: equal and of the same type. Suppresses conversions, so "42" === 42 is false.
    4. Also !==
  13. Exceptions
    1. throw. You may throw any type.
    2. The system and libraries throw Error or its descendents.
    3. The try/catch has the familiar syntax, without a type in the catch.
    4. Finally. Follows catch, and is run last. Does not take an argument.
      try { // Attempt something } catch(debris) { // No type declaration! Not java // Handle error } finally { // Any cleanup that applies to either outcome. }
  14. Control flow.
    1. Curly braces for blocks, as Java.
    2. if else
    3. while, do ... while
    4. for()
    5. Statements may be labeled
      foo: m = 10
    6. break, continue. These may take a label, so you can break (or continue) a loop other than the nearest one.
    7. switch, using break, like Java. Unlimited value type.
    8. for...in and for...of, which we'll discuss later.
  15. Strings
    1. As in Java, Strings are a type of object.
    2. Constants with 'single quotes' or "double quotes". Delimiter may be escaped with \ to include it.
      1. End with the same symbol you started with.
      2. That allows a string constant to easily contain the other delimiter without escape.
      3. No other difference (unlike PHP).
    3. Template strings can include expression values.
      1. Back-tick delimeters.
      2. The $ introduces a (non-literal) expression. The expression value becomes part of the string.
        let a = 19; let b = 5; let c = `a is ${a}, and b is ${b}, so a+b = ${a+b}`
      3. Now c is "a is 19, and b is 5, so a+b = 24"
    4. Subscript with brackets or .at()
    5. length property.
    6. Concat with +
    7. .contains, .startsWith(), .endsWith(), .indexOf(), .slice(), .toUpperCase(), .toLowerCase()
    8. .split(delim) to produce an array
  16. Arrays.
    1. Arrays are a kind of object.
    2. var arr = [ 4, 9, 3, 17 ]
    3. length property.
    4. Subscript [].
      1. Out of bounds okay.
      2. Fetch yields undefined
      3. Store expands the array and fills with undefined.
    5. .push(), .pop(), .shift(), .unshift()
    6. .join(delim) to produce a string.
    7. .forEach(function)
    8. .map(function)
    9. Non-number and negative subscripts are treated as hash keys. (Kind of nuts. PHP is nuts in a similar way.)
  17. Functions
    1. Classic definition (anonymous function)
      function (arg1, arg2, ...) { statements }
    2. New definition (anonymous function)
      (arg1, arg2, ...) => expression
      (arg1, arg2, ...) => { statements }
      In the first case, the expresison is automatically returned. In the second, you need a return statement to return a value.
    3. JavaScript uses anonymous functions often, but naming them is simple.
      let f1 = name(arg1, arg2, ...) { statements }
      let f2 = (arg1, arg2, ...) => { statements }
    4. And the classic form, which looks more like other scripting languages:
      function f3 (arg1, arg2, ...) { statements }
    5. Arguments passed in the same way Java does, by value for simple things and by reference value for objects.
    6. Functions may be called with fewer arguments than parameters, and the extra parameters have the value undefined.
    7. Default values are allowed.
      1. Default is used instead of undefined if the parameter is not sent. function(a = 4, b, c = 'foobar').
      2. Note that any parameter may (or not) have a default; no rule about only on the right as in C++.
    8. Variadic parameter lists are supported: function(a,b, ...rest) { }
      Assigns the third and following parameter to the array-like variable rest (any variable name).
    9. Functions have a special array-like variable arguments
      1. Can be used as an array, with subscripts from zero.
      2. Contains all arguments sent, regardless parameter names.
        function addem() { let s = 0; for (var p of arguments) { s += p }; return s; }
    10. Functions created with function are hoisted like vars. Functions created with => are not, and also do not have an arguments variable. The arrow syntax is newer, and, apparently, cooler.
  18. Simple Objects
    1. Object constants are key/value lists, much like hashes in other languages.
    2. var x = { frank: 10, bill: 30, sally: "string" };
    3. Reference with dot notation: x.bill
    4. Objects can have methods, but that's for another set of notes.
  19. Context.
    1. Every Javascript program runs in a context. In the browser, each window has a context.
    2. The context is represented by an implicit object, which you can refer to as this when not obviously running in some other object.
    3. Contents can be referred to as this.name or just name.
    4. Every context will contain language basic types, such as String, and their methods.
    5. A browser window context will also contain:
      1. Self-references, alias for root, such as window.
      2. References to roots of related windows, such a parent or child frames.
      3. Built-in functions, including for controlling the window.
      4. Built-in basic classes, such as String and Math.
      5. location object. Current URL and some other things.
      6. history object. Limited use in current JS for security reasons.
      7. screen object. Display parameters.
      8. document object. The document tree, or DOM tree. Where the action is.
        1. document.head
        2. document.body
        3. .children[i]
      9. Some browsers add all elements having a name or id attribute, so you can refer to DOM objects directly by their id.