Statements.JavaScript statementsoftenstartwithastatementidentifierwhile(i<10)if (i<10)1I else if else[for(i=0;i<10; i++)人16
6 Statements • JavaScript statements often start with a statement identifier for (i =0; i < 10; i++ ){ . } while (i < 10){ . } if (i < 10){ . } else if{ . } else{ . }
Statementstry [switch(i)llcodetotrytoruncase 1:1catch(error)break;...Il codetohandleerrorscase 2:1finally(break;../lcodeexecutedregardlessofdefault:thetry/catchresult..1
7 Statements switch (i){ case 1: . break; case 2: . break; default: . } try { . //code to try to run } catch(error){ .// code to handle errors } finally { . // code executed regardless of the try / catch result }
MathMathobjectallowsyoutoperformmathematicaltasks.Mathmethods:.Math.Pl;JavaScriptMathConstants.Math.pow(2,3);:Math.sqrt(64);3.1415926535897938:Math.abs(-3.17);8:Math.ceil(3.17);3.17A:Math.floor(3.17);3:Math.randomO);0.4022287665373549555.Math.max(0,10,6,55);8
8 Math • Math object allows you to perform mathematical tasks. • Math methods: • Math.PI; • Math.pow(2,3); • Math.sqrt(64); • Math.abs(-3.17); • Math.ceil(3.17); • Math.floor(3.17); • Math.random(); • Math.max(0,10,6,55);
Date·Dateobjectworkswithdatesandtimes. Four ways to instantiate a date::vard=newDate();:vard=newDate(milliseconds);llzerotimeis01Jan197000:00:00UTC:var d=newDate(dateString);:vard=newDate(year,month,day,hours,minutes,seconds,milliseconds);<IDOCTYPEhtmI>SunOct22201723:06:15GMT+0800(ChinaStandardTime)<html>FriJan02197008:00:00GMT+0800(ChinaStandardTime)<body>MonOct13201411:13:00GMT+0800(ChinaStandardTime)<p id="demo"></p><script>ThuJun24199911:33:30GMT+0800(ChinaStandardTime)var di - new Date();vard2-newDate(86400000);var d3-new Date("0ctober 13,201411:13:00");var d4=newDate(99,5,24,11,33,3,);document.getElementById("demo").innerHTMLd1+"<br>"+d2+"<bn>"+d3+"<br>"+d4;</script></body></html>9
9 Date • Date object works with dates and times • Four ways to instantiate a date: • var d = new Date(); • var d = new Date(milliseconds); //zero time is 01 Jan 1970 00:00:00 UTC • var d = new Date(dateString); • var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
DateMethods: d.getDateO;//getdayasanumber(1-31).d.getFullYearO;//get thefourdigityear(yyyy).d.setDate(15);//setdayasanumber(1-31): d.setFullYear(2100,0,14); I/sets a date object to a specific dateDate.parse("March21,2012");Il returnsthenumberofmillisecondsbetweenthedateandJanuary1,1970222017SunOct15201722:13:49GMT+0800(China StandardTime)ThuJan142100 22:13:49GMT+0800(ChinaStandardTime)133225920000010
10 Date Methods • d.getDate(); //get day as a number(1-31) • d.getFullYear(); //get the four digit year(yyyy) • d.setDate(15); //set day as a number(1-31) • d.setFullYear(2100,0,14); //sets a date object to a specific date • Date.parse("March 21, 2012"); // returns the number of milliseconds between the date and January 1, 1970