Unformatted text preview:

JavascriptJavascriptJavascript Javascript ToolsToolsJavascript Javascript ConsoleConsoleDebuggerDebuggerDOM InspectorDOM InspectorNumbersNumbersNo real distinction between floating point and integers,No real distinction between floating point and integers,internally, all numbers are internally, all numbers are fpfpOctal integers begin with 0 and are followed by 0-7 (8,9Octal integers begin with 0 and are followed by 0-7 (8,9interpreted as decimals), these are not supported in the ECMAinterpreted as decimals), these are not supported in the ECMAstandard--you likely won't see an octalstandard--you likely won't see an octalHex integers begin with 0x, (0-9, a-f lower or uppercase), Hex integers begin with 0x, (0-9, a-f lower or uppercase), egeg0x10 is 16 in decimal0x10 is 16 in decimalOctal and hex numbers can be negative, but cannot have aOctal and hex numbers can be negative, but cannot have adecimal portion nor support scientific notation.decimal portion nor support scientific notation.You're likely to run into issues mainly where you use an illegalYou're likely to run into issues mainly where you use an illegalnumber unknowinglynumber unknowingly……..Invalid NumbersInvalid Numbers00.234 (00 marks octal, which can't have a00.234 (00 marks octal, which can't have adecimal)decimal)0x3.45 (0x marks hex, and decimals aren0x3.45 (0x marks hex, and decimals aren’’ttallowed)allowed)0378 (value of 378 even 0378 (value of 378 even tho tho marked as anmarked as anoctal, since 8 coerces interpretation as aoctal, since 8 coerces interpretation as adecimal number)decimal number)Zero and Special NumbersZero and Special NumbersNull is not a 0 (although it sometimes acts like itNull is not a 0 (although it sometimes acts like itisisUndefined values act differently (they are NAN,Undefined values act differently (they are NAN,or Not a Number)or Not a Number)+0 and -0 not the same+0 and -0 not the same+infinity and -infinity not the same+infinity and -infinity not the sameMathMath12how_old.html12how_old.htmlStandard Math functions supported (+, -, *, /,Standard Math functions supported (+, -, *, /,etc.)etc.)Use Use parseIntparseInt() or () or parseFloatparseFloat() method to treat() method to treatvariables as numbersvariables as numbersPrecedence is important!Precedence is important!var x = prompt ("What Year Is It?");var y = prompt ("What Year Were You Born","");document.write ("You're " + (parseInt(x)-parseInt(y)) + " years old");CoercionCoercionA variable of one type can be used as if it were another.A variable of one type can be used as if it were another.If there's a conflict, If there's a conflict, javascript javascript doesn't produce an exceptiondoesn't produce an exceptionstring+number goes to stringsstring+number goes to stringsbooleanboolean+string goes to strings+string goes to stringsnumber+number+boolean boolean goes to numbersgoes to numbersExplicit conversionsExplicit conversionsstring to an integer, use the string to an integer, use the parseInt parseInt method.method.string to a number, use the string to a number, use the parseFloat parseFloat method.method.CoercionCoercion15math_parse.html15math_parse.htmlJavascript Javascript and typingand typing can be a messcan be a mess<script type="text/<script type="text/javascriptjavascript" language="JavaScript">" language="JavaScript">var var x = prompt ("value of x","");x = prompt ("value of x","");// use // use parseFloatparseFloat() to convert x to a number() to convert x to a numbervar var x_x_fp fp = = parseFloatparseFloat(x);(x);document.write("x is has a type of "document.write("x is has a type of " + + typeof typeof x + "<x + "<br br />");/>");document.write("x_document.write("x_fp fp has a type of "has a type of " + + typeof typeof x_x_fp fp + "<+ "<br br />");/>");</script></script>Digression: Dealing with FormsDigression: Dealing with FormsIn these recentIn these recent examples, I've been promptingexamples, I've been promptingthe user for data via an alert box--bad ideathe user for data via an alert box--bad ideaYou can use an HTML form You can use an HTML form instead--javascriptinstead--javascriptcan read and assign values to form objectscan read and assign values to form objectsYou have to identify the objects, so name themYou have to identify the objects, so name themMath and StringsMath and StringsLike we saw inLike we saw in phpphp, , javascript's javascript's coercion cancoercion canlead to errorslead to errorsSee math2.html and math3.htmlSee math2.html and math3.html<SCRIPT LANGUAGE=JavaScript>var x = prompt ("value of x","");var y = prompt ("value of y","");document.write("x * y = " + x*y + "<BR>");document.write("x + y = " + x + y + "<BR>");</SCRIPT>Quick ReviewQuick ReviewAn object is just a collection of properties,An object is just a collection of properties,values, and methods given a namevalues, and methods given a nameMath ObjectMath ObjectMath is also an objectMath is also an object<script language="JavaScript" <script language="JavaScript" type="text/javascript"type="text/javascript">>document.write("The value of PI is: " + Math.PIdocument.write("The value of PI is: " + Math.PI + "<+ "<br br />");/>");document.write("A random number is: " + Math.random()document.write("A random number is: " + Math.random() + "<+ "<br br />");/>");document.write("The square root of 156 is: "document.write("The square root of 156 is: " + Math.+ Math.sqrtsqrt(156) + "<(156) + "<br br />");/>");document.write("The square root of 156 rounded is: "document.write("The square root of 156 rounded is: " + Math.round(Math.+ Math.round(Math.sqrtsqrt(156)) + "<(156)) + "<br br />");/>");</script></script>08Math_Object.htmlCreating your own objectCreating your own objectYou canYou can create an object withcreate an object with an "array" ofan "array" ofcharacteristics and valuescharacteristics and valuesWe'll look at this in more detail laterWe'll look at this in more detail later……// Create an object named // Create an object named objobjvar obj var obj = {= { name: "Carrot", name: "Carrot", "for": "Max", "for": "Max", details: { details: { color: "orange", color: "orange", size: 12 size: 12 }


View Full Document

UNC-Chapel Hill INLS 672 - Javascript

Download Javascript
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Javascript and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Javascript 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?