1 / 34

Javascript Essentials

Javascript Essentials. How do I write it??. Start Homesite Between the start and end BODY tags type: <script language =“Javascript” <!-- hide script from old browsers alert('Welcome to my Web Site!'); //--> </script> Save and Browse. *. Javascript Basics. Data “Types” Declarations

strange
Download Presentation

Javascript Essentials

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Javascript Essentials

  2. How do I write it?? • Start Homesite • Between the start and end BODY tags type: • <script language =“Javascript” • <!-- hide script from old browsers • alert('Welcome to my Web Site!'); • //--> • </script> • Save and Browse *

  3. Javascript Basics • Data “Types” • Declarations • ExpressionsData Storage

  4. Javascript Data Types • - default type • ordinal numbers • Use parseInt() • real numbers • Use parseFloat() • String • Integral • Floating

  5. 01000001 01000001 Characters and Integers • char (1 Byte) 256 possibilities, ASCII characters enclosed in single quotes: ‘B’ • int (2 or 4 Bytes) no decimal points no commas no special signs ($, ¥, ‰) 01000010 * * *

  6. Floating Point Number Data Types • Floating point numbers: signed or unsigned with decimal point • Types: differ in amount of storage space; varies with machine • § float (single precision)§ double§ long double * * *

  7. 1000 NameTypeLocationnum1 int 1000 -9999 Variables • / a place to store information/ contents of a location in memory / has an address in RAM/ uses a certain number of bytes *

  8. Identifiers • Valid examples: • x num1 num2 name row c237 index tax_rate Not valid: 1num bye[a] tax-rate tax rate OK: newPos newpos (beware of this!) * * *

  9. Identifiers

  10. Rules for Naming Identifiers • Can contain digits, letters, underscores • NO SPACES • Begin identifier name with a letter • Limit Identifiers to 20 characters * * *

  11. Variable Definition • Associates an identifier with its use • Can be done with an assignment • or must be done with documentation Identifiers should have meaningful names. - Reduces confusion - Very few one letter names are acceptable: i, j, k, l, x, y, z (others?) - Two letter names? PI * * * *

  12. Variable Definition • var identifier_list; • var i, k; • var taxRate = 0.087; • var len = 10, wid =20; <script language =“Javascript”> var name = “Joe”; alert('Welcome to my Web Site!‘+ name); </script> * * * *

  13. Literals & Constants • Literal values are stored in anonymous memory addresses by the compiler Literals: 'Y', 23.4, -1, "Hello" typed directly into the program as needed ex. y = 23.4 pi = 3.1416 * * *

  14. Operators • An operator is a symbol that causes the compiler to take an action. Categories: mathematical assignment boolean/logical etc. *

  15. Binary Arithmetic Operators • addition + • subtraction ¾ • multiplication * • division / • modulus % Unary Minus operator: x = -3; y = -x; *

  16. Arithmetic Operators

  17. Increment and Decrement

  18. Arithmetic Expressions Syntaxoperand operator operand • Example7 + 15 34 — 189 92 * 31 345 / 6.02 86 % 3 *

  19. 12/3 14/3 4 3 12 12 0 4 3 14 12 2 12%3 14%3 Integer Division & Modulus • The modulus operator yields the remainder of integer division. * *

  20. Modulus Examples • The modulus operator yields the remainder of integer division. • 18 % 4 is 2 13 % 4 is 117 % 3 is 2 35 % 47 is 3524 % 6 is 0 24 % 4 is 0 4 % 18 is 4 0 % 7 is 0 12 % 2.5 error 6.0 % 6 error Requires floating point division! Not the same. * * * *

  21. Operator Precedence

  22. Order of Operations 8 + 3 * 4 is ? • Show associativity to clarify. ( 8 + 3 ) * 4 is 44 8 + ( 3 * 4 ) is 20 ( ) * / + - P M DA S from left to right * *

  23. Order of Operations • Expression Value 10 / 2 * 3 10 % 3 - 4 / 2 5.0 * 2.0 / 4.0 * 2.0 5.0 * 2.0 / (4.0 * 2.0) 5.0 + 2.0 / (4.0 * 2.0) 15 -1 5.0 1.25 5.25 * * * * *

  24. 10 % 3 - 4 / 2 10 / 2 * 3 i i i i r r r r i r 5.0 * 2.0 / 4.0 * 2.0 5 * 2 / (4.0 * 2.0) i Evaluation Trees * *

  25. i r r r r r r r r Evaluation Trees 5.0 + 2.0 / (4.0 * 2.0) 5.0 * 2.0 / 4.0 * 2.0 (5 + 2) / (4.0 * 2.0)

  26. Beware! • The asterisk is required to indicate multiplication. • valid invalid 5*(8+3) 5(8+3) • (x-y)*(x+y) (x-y)(x+y)

  27. Assignment Operator The assignment operator (=)causes the operand on the left to take on the value to the right side of the statement. • This operator assigns from right to left.valid invalid name = “Sue” “sue” = name *

  28. Assignment StatementSyntax: variable = expression; • Examples: quiz1score = 14; lname = “Newman”; fname = “Alfred E.”; wname = fname + “ “ + lname; Do NOT use the word equals here. Use is assigned to or gets. *

  29. Storage Locations1. deposit = 234;2. balance = 1000;3. balance = balance + deposit; • 4a. Look up balance value4b. Look up deposit value4c. Add values4d. Store result in balance *

  30. 6 bytes of storage q Storage Locations deposit balance 2 3 4 100 After assignment: balance = balance + deposit; 234 1234 e r t y\ w

  31. Assignment Operators

  32. String Concatenation

  33. Relational and Boolean Operators

  34. Conditional Statement (if)

More Related