Intro to JavaScript

  1. History
    1. Prior to 1995, Netscape was working on a significant update to their popular browser
    2. A new language called Java was being developed by Sun Microsystems, and Sun engineers were working with Netscape to make small Java programs called applets execute in a rectangular region of a web page
    3. Netscape was also developing a scripting language called LiveScript which could be used on the client to make web pages more dynamic and interactive, and on the server to handle common server tasks
    4. LiveScript was renamed JavaScript, perhaps as a marketing ploy to ride the popularity wave of Java (JavaScript has as much in common with Java as it does C++)
    5. Netscape 2 (released in 1995) supported JavaScript, and it became popular very quickly
    6. Microsoft added JavaScript support to IE 3.0 in 1996 and called their version JScript to avoid trademark issues
    7. JavaScript was standardized in 1997 by Ecma International which called their version ECMAScript
      "ECMAScript was always an unwanted trade name that sounds like a skin disease."
      - Brendan Eich, the creator of JavaScript (Source)
    8. ECMAScript 6 (aka ES6) major release in 2015
    9. ECMAScript 7 (aka ECMAScript 2016) minor relase in 2016, and browser support is coming along

      JavaScript: The Good Parts

  2. Today
    1. All major browsers support JavaScript, including all smartphone browsers
    2. JavaScript is used for Ajax (Asynchronous JavaScript and XML), a key component in most web applications
    3. HTML5 adds a number of new features to JavaScript to give it additional functionality needed for heavy-duty web applications, like saving database-like information inside the browser
    4. A number of JavaScript frameworks (AngularJS, Backbone) and libraries (jQuery, Underscore, React) simplify common tasks in JavaScript and augment the user interface
    5. JavaScript is increasingly being used outside the browser (e.g., node.js and Windows Universal apps)
  3. Traditional "types" of programming languages: compiled and interpreted
    1. Compiled languages
      1. Programming languages like C++, Java, and C# are compiled languages, meaning they are converted into machine code or something close to it by a compiler
      2. All syntax errors must be eliminated before the compiler can produce an executable program
      3. Usually a user runs the compiled version of the program and does not need access to the source code
    2. Interpreted languages
      1. Programming languages like JavaScript, PHP, Python, and Perl are not compiled but are instead interpreted by an interpreter at run-time (there is no executable)
      2. Syntax errors are not caught by the interpreter until run-time which can sometimes make debugging more difficult than compiled languages
      3. Usually a user must have the source code to run the script
      4. Interpreted languages usually execute much slower than compiled languages
    3. The line is blurring
      1. Today some languages may be interpreted or compiled, so it is more accurate to to say a particular implementation of a language is interpreted or compiled
      2. Many implementations (including some JavaScript engines) compile the language at run-time into an intermediate form to be executed in a virtual machine
      3. Some argue that the interpretation of the intermediate form makes the implementation "interpreted"
  4. Example JavaScript program
    <script type="text/javascript">
    document.writeln("Hello, <b>JavaScript</b>!");
    1. Browser will execute the output statement and display "Hello JavaScript!"
    2. <script> tags can be placed in the <head> or <body>
    3. type attribute defaults to text/javascript and can be omitted
    4. document object represents the document (web page) being displayed
    5. writeln() method outputs HTML to the document
    6. Semicolons are typically used to terminate statements but are not always required (see semicolon debate)
    7. JavaScript is case sensitive, so Document.WriteLn() is a syntax error
    8. Syntax errors will result in your script silently failing
      1. In Chrome, check the JavaScript Console (Ctrl-Shift-J) for error messages
      2. JavaScript Console can also be used to execute JavaScript on-the-fly