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(...).
    3. Note: Console output from code in a web page does not appear on the page. It appears in the (separate) console window, and is useful for debugging.
  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 if 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(init; test; incr) (counted for loop)
    5. break, continue.
    6. switch, using break, like Java. Unlimited value type.
    7. for...in and for...of
      1. These are different kinds of for-each; syntax differs from Java or C++.
      2. The for..of is most like the Java or C++ for each loop. It goes through the contents of an array or array-like object.
      3. The for..in goes through the keys or subscripts of a JavaScript object or array.
      4. We'll pick them up again under Arrays and Objects.
  15. Strings
    1. As in Java, Strings are objects.
    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 between the delimiter types, unlike some other languages.
    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. let arr = [ 4, 9, 3, 17 ]
    3. for..of:
      let fred=[4,5,22,1,34] for (n of fred) console.log(n)
      Prints 4, 5, 22, 1, 34, as you might expect.
    4. length property (not a method, same as Java).
    5. Subscript [].
      1. Out of bounds okay.
      2. Fetch yields undefined
      3. Store expands the array and fills with undefined.
      4. Non-number and negative subscripts are treated as hash keys. (Kind of nuts. PHP is nuts in a similar way.)
        > let fred = [3,9,"boo",8] undefined > fred[1] 9 > fred[7] undefined > fred[2]="joe" 'joe' > fred[6]="mike" 'mike' > fred [ 3, 9, 'joe', 8, <2 empty items>, 'mike' ]
    6. .push(), .pop(), .shift(), .unshift()
    7. .join(delim) to produce a string.
    8. .forEach(function)
    9. .map(function)
  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 value is automatically returned. In the second, you need a return statement if you want to return a value. You'll get undefined if there's no return statement.
    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 named function form, which looks more like other scripting languages:
      function f3 (arg1, arg2, ...) { statements }
    5. Arguments are 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. Usual reference with dot notation: x.bill
    4. Subscript (key as string) is also allowed: x["bill"] does the same thing.
    5. The for..in loop goes through the keys
      > let zippy = { a: 10, b: 47, tank: "boom" } > for(z in zippy) console.log(z+": "+zippy[z]) a: 10 b: 47 tank: boom
    6. Objects can have methods (like String and Array), and they are called as you would expect, obj.m(17).
    7. Making real objects is 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.