Numbers can be written with or without decimals Example var x13400 Written with

# Numbers can be written with or without decimals

• Homework Help
• ContentPro
• 51

This preview shows page 33 - 45 out of 51 pages.

Numbers can be written with, or without decimals: Example var x1=34.00; // Written with decimals var x2=34; // Written without decimals . Extra large or extra small numbers can be written with scientific (exponential) notation: Example var y=123e5; // 12300000 var z=123e-5; // 0.00123 33 JavaScript Numbers(cont..) <!DOCTYPE html> <html> <body> <script> var x1=34.00; var x2=34; var y=123e5; var z=123e-5; document.write(x1 + "<br>") document.write(x2 + "<br>") document.write(y + "<br>") document.write(z + "<br>") </script> </body> </html> 34 JavaScript Booleans Booleans can only have two values: true or false. var x=true; var y=false; Booleans are often used in conditional testing. 35 JavaScript Arrays(1/2) The following code creates an Array called cars: var cars=new Array(); cars="Saab"; cars="Volvo"; cars="BMW"; or (condensed array): var cars=new Array("Saab","Volvo","BMW"); or (literal array): Example var cars=["Saab","Volvo","BMW"]; 36 JavaScript Arrays(2/2) <!DOCTYPE html> <html> <body> <script> var i; var cars = new Array(); cars = “Nissan"; cars = “Toyota"; cars = "BMW"; for (i=0;i<cars.length;i++) { document.write(cars[i] + "<br>"); } </script> </body> </html> 37 The <script> Tag To insert a JavaScript into an HTML page, use the <script> tag. The <script> and </script> tells where the JavaScript starts and ends. The lines between the <script> and </script> contain the JavaScript: <script> alert("My First JavaScript"); </script> 38 JavaScript in <body> In this example, JavaScript writes into the HTML <body> while the page loads: Example <!DOCTYPE html> <html> <body> <script> document.write("<h1>This is a heading</h1>"); document.write("<p>This is a paragraph</p>"); </script> </body> </html> 39 JavaScript Functions and Events The JavaScript statements, in the example above, are executed while the page loads. More often, we want to execute code when an event occurs, like when the user clicks a button. If we put JavaScript code inside a function, we can call that function when an event occurs. 40 JavaScript in <head> or <body> You can place an unlimited number of scripts in an HTML document. Scripts can be in the <body> or in the <head> section of HTML, and/or in both. It is a common practice to put functions in the <head> section, or at the bottom of the page. This way they are all in one place and do not interfere with page content. 41 A JavaScript Function in <head> In this example, a JavaScript function is placed in the <head> section of an HTML page. The function is called when a button is clicked: 42 A JavaScript Function in <head>(cont.) <!DOCTYPE html> <html> <head> <script> function myFunction() { document.getElementById("demo").innerHTML="My First JavaScript Function"; } </script> </head> <body> <h1>My Web Page</h1> <p id="demo">A Paragraph.</p> <button type="button" onclick="myFunction()">Try it</button> </body> </html> 43 A JavaScript Function in <body> In this example, a JavaScript function is placed in the <body> section of an HTML page.  • • • 