Statements (p25)

        document.write('hello world');
        alert('yikes');
    

Write a statement, enter a semicolon, press Return to create a new blank line, type another statement, followed by a semicolon...etc
Notes

(Built-In) Functions (p26)

Web browsers have various commands to make things happen called functions . They are like verbs in a sentence.

   alert();
is a function
   document.write();
   prompt();

are also a functions. You can identify a function by the use of parenthesis.

Often, you pass an argument to the function inside the parenthesis.

   alert('hello world');
Functions generally do something complicated (than what you would expect a single line of code to do). In the above example, hello world is an argument




You will learn how to define your own functions. The functions provide by the system are called built-in functions.
Notes

Data Types (Textbook: Types of Data) (p27)

Programming languages categorize information into different types and treat each type differently.



Numbers:
Numbers used for counting and calculating.
  5            // integer numbers
  5.25         // floating point numbers (fractional numbers)
  10.3333333   // floating point numbers 
  -100         // negative integer number
  -2.301       // negative floating point number 
To print the total value of 5 plus 15, you could use the + operator (you'll learn about operators later) and do something like this:
   document.write(5 + 15);


Strings:
A series of letters and other symbols enclosed inside quote marks. You can use single quotes or double quotes.
'hello world'    // is a string
"Internet Class" // is a string
'1'              // is a string (not a number)
"as;kl29/P(983a" // is a string
hello world      // is NOT a sting 

// Read "Putting Quotes into Strings" (p28) for details
"Jane said "good day!" to John"    // this is not correct
'Jane said "good day!" to John'    // this is correct
"Jane said \"good day!\" to John"  // this is correct


Booleans:
It is either one of two values: true or false. Used to test for conditions like "Did the user type in a valid e-mail address?"
  true     // special keyword representing a boolean value true
  false    // special keyword representing a boolean value false
Strictly speaking, in most programming languages true/false is interpreted as NOT OFF (true) or OFF. Thus any non-zero value is interpreted as true and a zero is interpreted as false.
Notes

What is a Variable (p29)

Area of the square?



x, and y (which should be familiar to you from Mathematics) are essentially variables

A variable is a container (basket) with a name. In the follwing example, x is a variable













You can reuse the variable over and over. You can manipulate it. You can read what is in it and write into it. (eg. store the score of a game).

Why reusability is important:
  alert('Hi Bob');
The above statement only makes sense if everyone who visits the website is named Bob. You'd want your web page to present different names 'Hi Paul', 'Hi Jane'...etc. You want to do something like this:
  alert('Hi ' + x); 
Notes

Creating/using a variable (p30)

First you need to declare and name a variable.
  var score;     //declare and name a variable "score"
var is a keyword that creates (declares) the variable. The second part of the statement names the variable, score.

A note on naming variables
  • Variable names must begin with a letter, $, or _
  • Variable names can only contain letters, numbers, $, and _
  • Variable names are case-sensitive (SCORE and score are diferent variables)
  • Avoid keywords (for example, 'var' or 'alert'). See page 31 of textbook.
Notes

Using Variables (p32)

Once you create a variable, you can store any type of data. Use the "=" sign. "=" assigns the right side to the left side.
  var my_score;    // declare
  my_score = 0;    // the symbol "=" means assign. So, assign 0 to "score".
You can shorten the above two lines into one
  var your_score = 0;    // declare and assign value
  your_score = 1;        // reuse the variable
You can store strings, numbers, and Boolean values
/* numbers */
  var myAge = 25;     
  var myTemp = 70.5;  

  /* strings */
  var firstName = 'Peter';
  var lastName = 'Parker';
  var myAddress = "3203 SE Woodstock Ave";
  var myCity = "Portland";

  /* boolean: either true or false (or 1 or 0) */
  var student = true;  

Once you assigned a value to a variable, you can access the value stored simply by using the variable's name.
  alert(firstName);   // This will print the string stored - Peter

  alert('firstName'); // Note that this will print the string 'firstName'
Notes

Working with data types and variables (p33)

Math:
operator what it does expression result
+ addition 5 + 1 6
+ concatenation "Reed" + "  " + "College" "Reed College"
- subtraction 4 - 2 2
* multiplication 2 * 3 6
/ division 6 / 3 2
% modulus (remainder) 10 / 3 1


  var price = 10;
  var itemsOrdered = 15;
  var totalCost = price * itemsOrdered; 

Note that not all operations are equal. *, /, % take precidence over + and -.

  4 + 5 * 10 // is equivalent to 4 + (5 * 10)
To make sure the math works out the way you intend, use parentheses to group operations
  (4 + 5) * 10
Variables are reusable.
var score = 0;
score = 10;
score = score + 100;  //score will have 110 after this
score = score * 100;  //score will have 11000 after this
score = score / 100;  //score will have 110 after this

In fact this kind of operation is so common there are a shortcuts like these:

socre += 10;  // score = score + 10
score -= 10;  // score = score - 10;
score *= 10;  // score = score * 10;
score /= 10;  // score = score / 10;

score++;      // score = score + 1;    
score--;      // score = score - 1;

Notes

Combining (Concatenating) Strings (p36)

In addition to addition, the + operator can be used to concatenate strings together.
var firstName = 'John';
var lastName = 'Smith';
var fullName = firstName + lastName; // JohnSmith

var fullName = firstName + ' ' + lastName // John Smith
Notes

Combining Numbers and Strings (p37)

Here, the + operator convers the number to a string and concatenate it with the other string. (Otherwise it woudl try to add 10 to a string, which doesn't make sense).


var score = 10;
var message = 'You scored ' + score + ' points.';  // You scored 10 points.

This is called automatic type conversion and can be tricky when adding.

var price = '5'; // user input is usually a string
var shippingCost = 20;

var totalCost = price + shippingCost; // 520

To prevent this, put a + sign with no space inbetween before the variable. This tells the interpreter to try to convert the string to a number.
var price = '5'; 
var shippingCost = 20;

var totalCost = +price + shippingCost; // 25
or you could use the Number() function.
var price = '5'; 
var shippingCost = 20;

var totalCost = Number(price) + shippingCost; // 
Since there are no concatenation when using the * multiplication operator, the JavaScript will convert it to a number whenever possible.
Notes

Lab

- Read Ch 2 up to page 44.
- Ch2:Tutorial: Using Variables to Create Messages
- Ch2:Tutorial: Asking for Information (this explains the prompt() function)
- Using alert() and prompt(), create a website that converses with the user. Example [here].
Notes