Javascript II Expressions and Data Types Java Script





























- Slides: 29

Javascript II Expressions and Data Types

Java. Script Review l l programs executed by the web browser programs embedded in a web page l l using the script element programs consist of statements l executed sequentially 2

Statements l l l End with semi-colon Cannot occupy multiple lines Assignment statement l l l var = value; The “value” on the right can be any legal expression Function call l document. write ("foo"); 3

Variables l l A variable is a named location where a value can be stored In Java. Script l l l Other languages l l using a variable creates it can also declare require that variables be declared before being used Variables have "scope" l l locations in the program where they are valid will discuss this later on 4

Variable declaration l l Declaration is optional in Javascript Syntax var foo; l Says that foo is a variable we will use later 5

Data types l What is the difference between the following statements l l l val 1 = "53"; val 2 = 53; These values are different to Java. Script l l l stored differently manipulated differently same operation may mean something different l l + on strings in concatenation + on numbers is addition 6

Data Type l l A characterization of a stored value Determines l l what kinds of values can be stored how the value is stored internally what operations can be applied Syntactic representation l how the value is expressed in a program 7

Java. Script Types l l l Strings Integers Floats l l real numbers Boolean l true or false 8

Strings l What values l l l What operations l l lists of characters any length including the zero-length “null” string “” concatenation (+ operator) output to the Web page l using document. write(…) returned by prompt() function Syntax l double or single quotes 9

Examples l l l l val 1 = “Hello"; val 2 = ‘ there'; val 3 = val 1 + val 2; document. write (val 3); val 4 = “ 45”; val 2 = val 3 + val 5; document. write (val 2); 10

Integers l What values l l What operations l l l standard mathematical operations (+, -, *, /) special math functions Syntax l l whole numbers between -9223372036854775808 and 9223372036854775808 unquoted integers Note l book doesn't use integers in examples 11

Examples l l val 1 = 45; val 2 = 055; val 3 = val 1 + val 2; Math. sqrt(val 3); 12

Float l What values l l l What operations l l l decimal values from ± 1. 0 x 10308 to ± 1. 0 x 10 -323 17 digits of precision (past decimal point) standard mathematical operations special math functions Syntax l l unquoted decimal values scientific notation l 1. 2 e 3 = 1. 2 x 103 = 1200 13

Examples l l val 1 = 5. 3; val 2 = 5. 3 e 1; val 3 = val 1 + val 2; Math. floor(val 3); 14

Boolean l What values l l true / false What operations l logical operations l l and && or || not ! Syntax l keywords (true, false) 15

Examples l l l val 1 = true; val 2 = false; val 3 = val 1 && !val 2; 16

Another kind of value <script type="text/javascript"> var foo; document. write (foo); </script> l What is the value of foo? 17

undefined value l This is the value of a variable when it is created l l if you use a variable without defining it, you get an error You can declare a variable without giving it a value l l l not an error variable has no value unexpected results 18

Expressions l An expression is l l l a legal combination of Javascript values, operators, function calls and variables that evaluates to a value Examples l l l a+b 5/6 3. 14159 * r Math. sqrt (error. Term) "foo" + "-" + "bar" 19

Syntax alert l An expression is not a statement l l l an expression may be part of a statement note: no semi-colon Example l (a + b) / 2 l l expression size = a + b; l statement 20

Evaluation l An expression is evaluated l l when it is executed (run-time) Steps l l l Each variable is replaced by its current value Operators are applied Until a single value remains 21

Example l l l a = 5; b = 20; position = (a * 5) + (b / 10); 22

Complex expression l Expressions can be large and complex l l l Java. Script doesn't care Readers of your program might care A complex expression can always be simplified l by using intermediate variables 23

Example l complex expression l l slope = ( (y 1 – y 2) / (x 1 – x 2) ); simplified l l l delta. Y = y 1 – y 2; delta. X = x 1 – x 2; slope = delta. Y / delta. X; 24

The "+" trap l + means different things for different types l l "foo" + "bar" "foobar" "5" + "6" "56" 5 + 6 11 What about? l l "5" + 6 6 + "foo" 25

Operators l l + is an operator An operator l l l Some operators are two characters l l takes two values (sometimes one) makes ("returns") a new value && Assignment is not an operation l = is not an operator! 26

Functions l Like an operator l l takes values does some operation returns a result But l l l has a name instead of symbol can take any number of values can be user-defined 27

Predefined Functions l You can define your own functions l l later in the class Many built-in l l prompt document. write 28

Function syntax l prompt ( "Enter a number", "0") function name parameter #1 parameter #2 parameter list l l return value is a string containing the user input No parameters? l l Math. random () there's still a list, just an empty one 29