Javascript IIJavaScript ReviewStatementsVariablesVariable declarationData typesData TypeJavaScript TypesStringsExamplesIntegersSlide 12FloatSlide 14BooleanSlide 16Another kind of valueundefined valueExpressionsSyntax alertEvaluationExampleComplex expressionSlide 24The "+" trapOperatorsFunctionsPredefined FunctionsFunction syntaxJavascript IIExpressions andData Types2JavaScript Reviewprograms executed by the web browserprograms embedded in a web pageusing the script elementprograms consist of statementsexecuted sequentially3StatementsEnd with semi-colonCannot occupy multiple linesAssignment statementvar = value;The “value” on the right can be any legal expressionFunction calldocument.write ("foo");4VariablesA variable is a named location where a value can be storedIn JavaScriptusing a variable creates itcan also declareOther languagesrequire that variables be declared before being usedVariables have "scope"locations in the program where they are validwill discuss this later on5Variable declarationDeclaration is optional in JavascriptSyntaxvar foo;Says that foo is a variable we will use later6Data typesWhat is the difference between the following statementsval1 = "53";val2 = 53;These values are different to JavaScriptstored differentlymanipulated differentlysame operation may mean something different+ on strings in concatenation+ on numbers is addition7Data TypeA characterization of a stored valueDetermineswhat kinds of values can be storedhow the value is stored internallywhat operations can be appliedSyntactic representationhow the value is expressed in a program8JavaScript TypesStringsIntegersFloatsreal numbersBooleantrue or false9StringsWhat valueslists of charactersany length including the zero-length “null” string “”What operationsconcatenation (+ operator)output to the Web pageusing document.write(…)returned by prompt() functionSyntaxdouble or single quotes10Examplesval1 = “Hello";val2 = ‘ there';val3 = val1 + val2;document.write (val3);val4 = “45”;val2 = val3 + val5;document.write (val2);11IntegersWhat valueswhole numbers between -9223372036854775808 and 9223372036854775808What operationsstandard mathematical operations (+, -, *, /)special math functionsSyntaxunquoted integersNotebook doesn't use integers in examples12Examplesval1 = 45;val2 = 055;val3 = val1 + val2;Math.sqrt(val3);13FloatWhat valuesdecimal values from ±1.0x10308 to ± 1.0x10-32317 digits of precision (past decimal point)What operationsstandard mathematical operationsspecial math functionsSyntaxunquoted decimal valuesscientific notation1.2e3 = 1.2 x 103 = 120014Examplesval1 = 5.3;val2 = 5.3e1;val3 = val1 + val2;Math.floor(val3);15BooleanWhat valuestrue / falseWhat operationslogical operationsand &&or ||not !Syntaxkeywords (true, false)16Examplesval1 = true;val2 = false;val3 = val1 && !val2;17Another kind of value<script type="text/javascript">var foo;document.write (foo);</script>What is the value of foo?18undefined valueThis is the value of a variable when it is createdif you use a variable without defining it, you get an errorYou can declare a variable without giving it a valuenot an errorvariable has no valueunexpected results19ExpressionsAn expression is a legal combination of Javascript values, operators, function calls and variablesthat evaluates to a valueExamplesa + b5 / 63.14159 * r * rMath.sqrt (errorTerm)"foo" + "-" + "bar"20Syntax alertAn expression is not a statementan expression may be part of a statementnote: no semi-colonExample(a + b) / 2expressionsize = a + b;statement21EvaluationAn expression is evaluated when it is executed (run-time)StepsEach variable is replaced by its current valueOperators are appliedUntil a single value remains22Examplea = 5;b = 20;position = (a * 5) + (b / 10);23Complex expressionExpressions can be large and complexJavaScript doesn't careReaders of your program might careA complex expression can always be simplifiedby using intermediate variables24Examplecomplex expressionslope = ( (y1 – y2) / (x1 – x2) );simplifieddeltaY = y1 – y2;deltaX = x1 – x2;slope = deltaY / deltaX;25The "+" trap+ means different things for different types"foo" + "bar" "foobar""5" + "6" "56"5 + 6 11What about?"5" + 66 + "foo"26Operators+ is an operatorAn operatortakes two values (sometimes one)makes ("returns") a new valueSome operators are two characters&&Assignment is not an operation= is not an operator!27FunctionsLike an operatortakes valuesdoes some operationreturns a resultButhas a name instead of symbolcan take any number of valuescan be user-defined28Predefined FunctionsYou can define your own functionslater in the classMany built-inpromptdocument.write29Function syntaxprompt ( "Enter a number", "0")return value is a string containing the user inputNo parameters?Math.random ()there's still a list, just an empty onefunction name parameter #1parameter #2parameter
View Full Document