JavascriptJavascriptJavascript Javascript ToolsToolsJavascript Javascript ConsoleConsoleDebuggerDebuggerDOM InspectorDOM InspectorNumbersNumbersNo real distinction between floating point and integers,No real distinction between floating point and integers,internally, all numbers are internally, all numbers are fpfpOctal 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 octalHex 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 decimalOctal 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 Numbers00.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 NumbersNull is not a 0 (although it sometimes acts like itNull is not a 0 (although it sometimes acts like itisisUndefined 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.htmlStandard Math functions supported (+, -, *, /,Standard Math functions supported (+, -, *, /,etc.)etc.)Use Use parseIntparseInt() or () or parseFloatparseFloat() method to treat() method to treatvariables as numbersvariables as numbersPrecedence 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");CoercionCoercionA 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 exceptionstring+number goes to stringsstring+number goes to stringsbooleanboolean+string goes to strings+string goes to stringsnumber+number+boolean boolean goes to numbersgoes to numbersExplicit conversionsExplicit conversionsstring 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.htmlJavascript 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 FormsIn 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 ideaYou 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 objectsYou have to identify the objects, so name themYou have to identify the objects, so name themMath and StringsMath and StringsLike we saw inLike we saw in phpphp, , javascript's javascript's coercion cancoercion canlead to errorslead to errorsSee 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 ReviewAn 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 ObjectMath 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 objectYou canYou can create an object withcreate an object with an "array" ofan "array" ofcharacteristics and valuescharacteristics and valuesWe'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