Javascript Language
  1. 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.
  2. 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.
  3. Same comments as Java.
  4. Semicolons are optional at the end of a line, but usually used.
  5. Displaying values.
    1. The console will print values of expressions.
    2. Values can be printed with console.log(...).
  6. 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 $.
  7. 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).
  8. Usual numeric operators, +, -, *, / and %, and update forms. Also ++ and --.
  9. 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.)
  10. 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 !==
  11. Exceptions
    1. throw. You may throw any type.
    2. try/catch.
    3. finally. Follows catch, and is run last.
  12. 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.
  13. 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
  14. 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.)
  15. 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 a function, with some technical differences we'll get to later.
      2. Can omit the brackets for one statements.
  16. 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.
  17. 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();
  18. And much more
Context