Javascript Language
  1. Name.
    1. As noted elsewhere, JavaScript was created by Netscape, and given its unfortunate name for marketing reasons.
    2. Standard Javascript is more properly called ECMAScript, for the ECMA which publishes the standard, ECMA-262.
    3. ECMA stands for the European Computer Manufacturers Association, but it now just calls itself ECMA International. It publishes several standards for the industry.
    4. The language mostly answers to JavaScript.
  2. 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.
  3. 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.
  4. Same comments as Java.
  5. Semicolons are terminators, as in C, Java and friends, but are implied by line breaks and some punctuation, so they may often be omitted. Better not to.
  6. Displaying values.
    1. The console will print values of expressions.
    2. Values can be printed with console.log(...).
  7. Variables
    1. Declared with var, and constants with const.
    2. Case sensitive.
    3. Dynamically typed.
    4. C-like scoping rules: Variables may be global, or belong to a scope enclosed in { } .
    5. Variable names.
      1. Start with letter or _ and contain letter, digit or _ as usual.
      2. Can also start with or contain $.
  8. 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).
  9. Usual numeric operators, +, -, *, / and %, and update forms. Also ++ and --.
  10. 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.)
  11. 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 !==
  12. 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. }
  13. 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.
  14. Strings
    1. As in Java, Strings are a type of object.
    2. Constants with 'single quotes' or "double quotes". End with the same start.
    3. Subscript with brackets or .at()
    4. length property.
    5. Concat with +
    6. .contains, .startsWith(), .endsWith(), .indexOf(), .slice(), .toUpperCase(), .toLowerCase()
    7. .split(delim) to produce an array
  15. 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(), ., .unshift()
    6. .join(delim) to produce a string.
    7. .map(function)
    8. Non-number and negative subscripts are treated as hash keys. (Kind of nuts. PHP is nuts in a similar way.)
  16. Functions
    1. Normal function definition
      function name(arg1, arg2, ...) { statements }
    2. Anonymous lambda function assigned to a variable
      var f = function(arg1, arg2, ...) { statements }
    3. The second is a lambda, which are becoming more common in many languages. This lambda closure is then assigned to a variable, but they are often passed as parameters.
    4. Arguments passed in the same way Java does, by value for simple things and by reference value for objects.
    5. Functions may be called with fewer arguments than parameters, and the extra arguments get undefined.
    6. Functions always have an arguments object, which is an array of all arguments sent.
    7. Functions may be called with more arguments than parameters. This can be used to access extras.
    8. Default values are allowed. Default is used instead of undefined if the parameter is not sent.
    9. function(a,b, ...rest)
      Assigns the third and following parameter to the array-like rest.
    10. var f = (arg1, arg2, ...) => { statements }
      1. Also creates an anonymous function.
      2. Can omit the brackets for one statements.
      3. Lighter implementation for efficiency. Leaves out some capabilities that you probably weren't using anyway.
  17. Object constants.
    1. Object constants are key/value lists, much like hashes in other languages.
    2. var x = { frank: 10, bill: 30, sally: "string" };
    3. x.bill
    4. Some fields can be functions, which are methods of the objects.
    5. Method functions refer to their own fields with this
    6. Far more to say about objects.
  18. Objects can be created by functions.
    function joe(i) { this.size = i; this.log = function() { console.log("My size is " + this.size); } } var j = new joe(12); j.log();
  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. Contents
      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.