350 likes | 411 Views
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
E N D
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 • ExpressionsData Storage
Javascript Data Types • - default type • ordinal numbers • Use parseInt() • real numbers • Use parseFloat() • String • Integral • Floating
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 * * *
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 * * *
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 *
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!) * * *
Rules for Naming Identifiers • Can contain digits, letters, underscores • NO SPACES • Begin identifier name with a letter • Limit Identifiers to 20 characters * * *
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 * * * *
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> * * * *
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 * * *
Operators • An operator is a symbol that causes the compiler to take an action. Categories: mathematical assignment boolean/logical etc. *
Binary Arithmetic Operators • addition + • subtraction ¾ • multiplication * • division / • modulus % Unary Minus operator: x = -3; y = -x; *
Arithmetic Expressions Syntaxoperand operator operand • Example7 + 15 34 — 189 92 * 31 345 / 6.02 86 % 3 *
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. * *
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. * * * *
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 * *
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 * * * * *
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 * *
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)
Beware! • The asterisk is required to indicate multiplication. • valid invalid 5*(8+3) 5(8+3) • (x-y)*(x+y) (x-y)(x+y)
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 *
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. *
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 *
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