•
You can use single or double quotes:
Example
•
var carname="Volvo XC60";
var carname='Volvo XC60';
•
You can use quotes inside a string, as long as they don't
match the quotes surrounding the string:
•
Example
•
var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';
22

JavaScript Numbers
•
JavaScript has only one type of numbers.
•
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
23

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>
24

JavaScript Booleans
•
Booleans can only have two values: true or false.
•
var x=true;
var y=false;
•
Booleans are often used in conditional testing.
•
25

JavaScript Arrays(1/2)
•
The following code creates an Array called cars:
•
var cars=new Array();
cars[0]="Saab";
cars[1]="Volvo";
cars[2]="BMW";
•
or (condensed array):
•
var cars=new Array("Saab","Volvo","BMW");
•
or (literal array):
•
Example
•
var cars=["Saab","Volvo","BMW"];
26

JavaScript Arrays(2/2)
•
<!DOCTYPE html>
•
<html>
•
<body>
•
<script>
•
var i;
•
var cars = new Array();
•
cars[0] = “Nissan";
•
cars[1] = “Toyota";
•
cars[2] = "BMW";
•
for (i=0;i<cars.length;i++)
•
{
•
document.write(cars[i] + "<br>");
•
}
•
</script>
•
</body>
•
</html>
27

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>
28

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>
29

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.
30

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.
31

A JavaScript Function in <head>
•
In this example, a JavaScript function is placed in the
<head> section of an HTML page.


You've reached the end of your free preview.
Want to read all 41 pages?
- Spring '12
- Guttag, John
- JavaScript code, John Doe, Volvo