Download presentation
Presentation is loading. Please wait.
2
Javascript Essentials
3
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!'); //--> Save and Browse *
4
Data “Types” Declarations Expressions Data Storage Javascript Basics
5
Javascript Data Types String Integral Floating - default type -ordinal numbers -Use parseInt() -real numbers -Use parseFloat()
6
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 01000001
7
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 * * *
8
Variables a place to store information contents of a location in memory * has an address in RAM uses a certain number of bytes -9999 1000 Name Type Location num1 int 1000
9
Identifiers Valid examples: xnum1num2 name rowc237indextax_rate * * * Not valid: 1num bye[a] tax-rate tax rate OK: newPos newpos (beware of this!)
10
Identifiers
11
Rules for Naming Identifiers Can contain digits, letters, underscores NO SPACES Begin identifier name with a letter Limit Identifiers to 20 characters * * *
12
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 * *
13
Variable Definition var identifier_list; var i, k; var taxRate = 0.087; var len = 10, wid =20; * * var name = “Joe”; alert('Welcome to my Web Site!‘+ name);
14
Literals & Constants Literals: 'Y', 23.4, -1, "Hello" typed directly into the program as needed ex. y = 23.4pi = 3.1416 Literal values are stored in anonymous memory addresses by the compiler * * *
15
Operators An operator is a symbol that causes the compiler to take an action. * Categories: mathematical assignment boolean/logical etc.
16
Binary Arithmetic Operators addition+ subtraction multiplication * division / modulus % Unary Minus operator: x = -3; y = -x; *
17
Arithmetic Operators
18
Increment and Decrement
19
Arithmetic Expressions Syntax operand operator operand Example 7+15 34— 189 92 *31 345 / 6.02 86 % 3 *
20
Integer Division & Modulus 12/3 14/3 12%3 14%3 The modulus operator yields the remainder of integer division. * 4 3 12 12 0 4 3 14 12 2
21
Modulus Examples The modulus operator yields the remainder of integer division. 18 % 4 is 2 13 % 4 is 1 17 % 3 is 2 35 % 47 is 35 24 % 6 is 0 24 % 4 is 0 4 % 18 is 4 0 % 7 is 0 * * Requires floating point division! Not the same. 12 % 2.5 error 6.0 % 6 error
22
Operator Precedence
23
Order of Operations 8 + 3 * 4 is ? Show associativity to clarify. ( 8 + 3 ) * 4 is 44 8 + ( 3 * 4 ) is 20 * P M D A S from left to right ( ) * / + -
24
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 5.0 1.25 5.25 * * * * *
25
Evaluation Trees i i 10 / 2 * 3 * 10 % 3 - 4 / 2 i i i 5.0 * 2.0 / 4.0 * 2.0 r r r 5 * 2 / (4.0 * 2.0) i r r
26
Evaluation Trees 5.0 * 2.0 / 4.0 * 2.0 5.0 + 2.0 / (4.0 * 2.0) (5 + 2) / (4.0 * 2.0) i r r r r r r r r
27
Beware! The asterisk is required to indicate multiplication. validinvalid 5*(8+3) 5(8+3) (x-y)*(x+y) (x-y)(x+y)
28
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. validinvalid name = “Sue”“sue” = name *
29
Assignment Statement Syntax: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. *
30
4a. Look up balance value 4b. Look up deposit value 4c. Add values 4d. Store result in balance * Storage Locations 1. deposit = 234; 2. balance = 1000; 3. balance = balance + deposit;
31
Storage Locations q e 234 234 2 3 4 100 1234 1234 w 6 bytes of storage deposit balance After assignment: balance = balance + deposit; r t y\
32
Assignment Operators
33
String Concatenation
34
Relational and Boolean Operators
35
Conditional Statement (if)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.