JavaScript 1 Getting Started
JavaScript 1 Getting Started
JavaScript:
Getting Started
Slides 2016 Marty Hall, [email protected]
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Intro
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Overview of JavaScript
Used primarily for Web pages (i.e., runs in browser)
To make them more interactive
To get updates from the server without reloading the page (Ajax)
Use growing in contexts other than Web browsers
Node.js for running JavaScript on server
Mozilla Rhino for running JavaScript on desktop
Simpler than most other programming languages (arguably)
So a good starting point for beginning programmers
Second-most popular language behind Java (arguably)
So many jobs available
Not closely related to Java
Despite the similar-sounding names
6
Y axis tracks percent of total job postings, not absolute numbers. So, for example, when all the curves go down, it is likely due to a stronger economy with many more teacher and construction jobs. Only the relative values are important.
JavaScript Popularity: Google Searches
https://www.google.com/trends/
Google is deliberately vague about their y-axis scale, but other testing shows it is not absolute number of searches, but some sort of relative scale.
Online References
JavaScript tutorial (language syntax)
https://developer.mozilla.org/en-US/docs/Web/JavaScript
http://www.w3schools.com/js/
JavaScript API references (builtin objects)
http://www.w3schools.com/jsref/
http://www.devguru.com/technologies/ecmascript/QuickRef/
http://www.devguru.com/technologies/JavaScript/
http://www.javascriptkit.com/jsref/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference
HTML DOM reference (with JavaScript Examples)
http://www.w3schools.com/htmldom/dom_reference.asp
Official ECMAScript specification
http://www.ecma-international.org/publications/standards/Ecma-262.htm
11
coreservlets.com custom onsite training
Interactive JavaScript
Practice
Slides 2016 Marty Hall, [email protected]
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
14
15
Configure Firebug: Details
Tell Firebug to use multi-line input editor (optional)
Click icon in bottom right of Console window
16
JavaScript Testing
Problem
Java: very strict compliance tests to be called Java
You can have very high confidence that code written in Java 8 on Windows version will
run identically (except for some differences in how GUIs look) on Java 8 on MacOS,
Linux, Solaris, and other Windows versions. True for Java from Oracle, Apple, IBM, or
open source version from Brazil.
JavaScript: every browser vendor makes own version, with no outside checks
Behavior of same JavaScript program can vary substantially from browser to browser,
and even from one release of the same browser to another
Consequence
Before final deployment, you must test on all browsers you expect to support
Most developers
Do initial testing and development on either Chrome or Firefox
But test also on Internet Explorer, Microsoft Edge, and Safari before final deployment
19
coreservlets.com custom onsite training
Variables
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Variables
Introduce with var
For global variables (!) and local variables.
No var for function arguments
You do not declare types
Some people say JavaScript is untyped language, but technically it is
dynamically typed language
JavaScript is very liberal about converting types
There are only two scopes
Global scope
Be very careful with this when using Ajax
Can cause race conditions
Function (lexical) scope
There is not block scope as in Java
21
Variables: Examples
var firstName = "Jane"; // String
firstName; "Jane"
var price = 23.7; // Number
var isDiscounted = true; // Boolean
var quarterlySales = [12, 18, 15, 9];
/* Array -- arrays are covered later */
var customer = { firstName: "Jane", lastName: "Doe" };
/* Object -- objects are covered later */
var x = 5;
function square(x) { return(x * x); }
x; 5
square(7); 49
x; 5
22
Operators
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Functions: Basics
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Functions
Quick summary now
No types for the parameters
Declare (global) functions with function
Use return if you want return values
Defining a function really just makes a variable whose value is a function
function() {
var x = 7;
}
Usage
Global-scope variables can be seen inside functions and on the outside
Function-scope variables can be seen inside their own function but not on outside
30 See interactive example
Wrap-up
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Summary
Use Firebug or Chrome for practice, testing, and debugging
Install Firebug from http://getfirebug.com/
Get a reference book
JavaScript the Definitive Guide (Flanagan, OReilly)
Bookmark online references
https://developer.mozilla.org/en-US/docs/Web/JavaScript
http://www.w3schools.com/js/
Load JavaScript file from Web page
<script src="scripts/some-script.js"></script>
Practice basic JavaScript syntax
Declare local variables with var
Use *, /, +, etc., and note + can be either addition or concatenation
Declare functions with function
33
coreservlets.com custom onsite training
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.