JavaScript: The Missing Manual Chapter 2: The Grammar of JavaScript Author: David S. McFarland | Publisher: O’Reilly Copyright 2010
Statements JavaScript statement: a basic programming unit, usually representing a single step in a JavaScript program combine statements to create a JavaScript program each statement ends with a semicolon like a period at the end of a sentence Copyright 2010
Commands Commands are usually called  functions  or  methods like verbs in a sentence, commands get things done Examples: alert(); document.write(); Chapter 3 will focus on  functions . Copyright 2010
Types of Data In JavaScript the three most common types of data are: numbers strings Boolean Copyright 2010
Numbers a  number  is used for counting or calculating Example: document.write(5 + 15) Result: 20 is written to the page Copyright 2010
Strings A  string  is simply a series of letters and other symbols enclosed inside of quote marks you can use either single or double quotes ‘ Welcome back!’ “ Welcome back!” Copyright 2010
Booleans A  Boolean  value is either true or false typically used to make decisions Example: The visitor filled in the “name” textbox . . . true or false? Copyright 2010
Variables A  variable  is a way to store information so that you can use and manipulate it variables  hold information that can vary Think of a variable as a container or basket the container remains the same even if you change the contents of the container Copyright 2010
Creating a Variable Two-step process: declare  the variable name  the variable Example: var score; Copyright 2010
Creating a Variable Rules:  A variable name . . . must begin with a letter, $, or _ can only contain letters, numbers, $, and _ is case sensitive must not be the same as a keyword (reserved words) Google “JavaScript keywords” Copyright 2010
Using Variables Once a variable is created you can store any type of data that you’d like in it var score; var name_first; score = 0; name_first = “Peter”; Copyright 2010
Using Variables Once a variable is created you can store any type of data that you’d like in it var score; score = 0; var score = 0; Copyright 2010 assignment operator
Data Types & Variables Operator  – a symbol or word that can change one or more values into something else used to modify data Copyright 2010
Basic Math Math Operators: Copyright 2010 Result Example Operator 3 15 / 5 / 50 5 * 10 * 20 25 - 5 - 30 5 + 25 +
Order of Operations Some operations take precedence over others . . . Multiplication (*) and division (/)   takes precedence over addition (+) and subtraction (-) Copyright 2010
Order of Operations Use parentheses to group operations Example: 4 + 5 * 10 result: 54 (4 + 5) * 10 result: 90 Copyright 2010
Combining Strings Concatenation  – combining strings by using the + operator var name_first = “Peter”; var name_last = “Kery”; var full_name = name_first + name_last; Copyright 2010
Combining Numbers &  Strings Copyright 2010
Changing the Values in  Variables Example: var score = 0; score = score + 100; Copyright 2010
Shortcuts for Performing  Math Copyright 2010 score = score + 1; score ++; ++ score = score / 10; score /= 10; /= score = score * 10; score *= 10; *= score = score - 1; score --; -- score = score - 10; score -= 10; -= score = score + 10; score += 10; += Alternate Example Operator
Tutorial #1 Using variables to create messages: open file 2.1.html add a <script> element create two variables for first and last name add three document.write statements to print out the full name in a paragraph 2.1.html Copyright 2010
Tutorial #2 Asking for information: open file 2.2.html add a prompt() command to first script:  var name = prompt(“What is your  ►   name?”, “”); 2.2.html Copyright 2010
Tutorial #2 Asking for information (continued): add a document.write statement to second script: document.write(“<p>Welcome” +  ► name + “</p>” ); 2.2.html Copyright 2010
Arrays Copyright 2010
Creating an Array Copyright 2010
Assessing Items in an  Array Copyright 2010
Adding Items to an Array Copyright 2010
Deleting Items from an  Array Copyright 2010
Adding & Deleting with  splice() Copyright 2010
Tutorial #3 Writing to a Web page using arrays 2.3.html Copyright 2010
Comments A  comment  is simply a line or more of notes interpreter ignores comments useful reminders for how the script works Copyright 2010
Comments Styles: // This is a comment. /* This is also a comment that covers multiple lines */ Copyright 2010
Comments Examples: // create a variable for first name. var name_first; // remember no SINs /* The purpose of this script is to capture the visitors name and print it back to the page. */ Copyright 2010
JavaScript blah, blah, blah . . . Copyright 2010

JavaScript Missing Manual, Ch. 2

  • 1.
    JavaScript: The MissingManual Chapter 2: The Grammar of JavaScript Author: David S. McFarland | Publisher: O’Reilly Copyright 2010
  • 2.
    Statements JavaScript statement:a basic programming unit, usually representing a single step in a JavaScript program combine statements to create a JavaScript program each statement ends with a semicolon like a period at the end of a sentence Copyright 2010
  • 3.
    Commands Commands areusually called functions or methods like verbs in a sentence, commands get things done Examples: alert(); document.write(); Chapter 3 will focus on functions . Copyright 2010
  • 4.
    Types of DataIn JavaScript the three most common types of data are: numbers strings Boolean Copyright 2010
  • 5.
    Numbers a number is used for counting or calculating Example: document.write(5 + 15) Result: 20 is written to the page Copyright 2010
  • 6.
    Strings A string is simply a series of letters and other symbols enclosed inside of quote marks you can use either single or double quotes ‘ Welcome back!’ “ Welcome back!” Copyright 2010
  • 7.
    Booleans A Boolean value is either true or false typically used to make decisions Example: The visitor filled in the “name” textbox . . . true or false? Copyright 2010
  • 8.
    Variables A variable is a way to store information so that you can use and manipulate it variables hold information that can vary Think of a variable as a container or basket the container remains the same even if you change the contents of the container Copyright 2010
  • 9.
    Creating a VariableTwo-step process: declare the variable name the variable Example: var score; Copyright 2010
  • 10.
    Creating a VariableRules: A variable name . . . must begin with a letter, $, or _ can only contain letters, numbers, $, and _ is case sensitive must not be the same as a keyword (reserved words) Google “JavaScript keywords” Copyright 2010
  • 11.
    Using Variables Oncea variable is created you can store any type of data that you’d like in it var score; var name_first; score = 0; name_first = “Peter”; Copyright 2010
  • 12.
    Using Variables Oncea variable is created you can store any type of data that you’d like in it var score; score = 0; var score = 0; Copyright 2010 assignment operator
  • 13.
    Data Types &Variables Operator – a symbol or word that can change one or more values into something else used to modify data Copyright 2010
  • 14.
    Basic Math MathOperators: Copyright 2010 Result Example Operator 3 15 / 5 / 50 5 * 10 * 20 25 - 5 - 30 5 + 25 +
  • 15.
    Order of OperationsSome operations take precedence over others . . . Multiplication (*) and division (/) takes precedence over addition (+) and subtraction (-) Copyright 2010
  • 16.
    Order of OperationsUse parentheses to group operations Example: 4 + 5 * 10 result: 54 (4 + 5) * 10 result: 90 Copyright 2010
  • 17.
    Combining Strings Concatenation – combining strings by using the + operator var name_first = “Peter”; var name_last = “Kery”; var full_name = name_first + name_last; Copyright 2010
  • 18.
    Combining Numbers & Strings Copyright 2010
  • 19.
    Changing the Valuesin Variables Example: var score = 0; score = score + 100; Copyright 2010
  • 20.
    Shortcuts for Performing Math Copyright 2010 score = score + 1; score ++; ++ score = score / 10; score /= 10; /= score = score * 10; score *= 10; *= score = score - 1; score --; -- score = score - 10; score -= 10; -= score = score + 10; score += 10; += Alternate Example Operator
  • 21.
    Tutorial #1 Usingvariables to create messages: open file 2.1.html add a <script> element create two variables for first and last name add three document.write statements to print out the full name in a paragraph 2.1.html Copyright 2010
  • 22.
    Tutorial #2 Askingfor information: open file 2.2.html add a prompt() command to first script: var name = prompt(“What is your ► name?”, “”); 2.2.html Copyright 2010
  • 23.
    Tutorial #2 Askingfor information (continued): add a document.write statement to second script: document.write(“<p>Welcome” + ► name + “</p>” ); 2.2.html Copyright 2010
  • 24.
  • 25.
    Creating an ArrayCopyright 2010
  • 26.
    Assessing Items inan Array Copyright 2010
  • 27.
    Adding Items toan Array Copyright 2010
  • 28.
    Deleting Items froman Array Copyright 2010
  • 29.
    Adding & Deletingwith splice() Copyright 2010
  • 30.
    Tutorial #3 Writingto a Web page using arrays 2.3.html Copyright 2010
  • 31.
    Comments A comment is simply a line or more of notes interpreter ignores comments useful reminders for how the script works Copyright 2010
  • 32.
    Comments Styles: //This is a comment. /* This is also a comment that covers multiple lines */ Copyright 2010
  • 33.
    Comments Examples: //create a variable for first name. var name_first; // remember no SINs /* The purpose of this script is to capture the visitors name and print it back to the page. */ Copyright 2010
  • 34.
    JavaScript blah, blah,blah . . . Copyright 2010