JavaScript gives HTML designers a
programming tool - HTML authors are normally
not programmers, but JavaScript is a scripting
language with a very simple syntax! Almost
anyone can put small "snippets" of code into
their HTML pages
JavaScript can put dynamic text into an HTML
page - A JavaScript statement like this:
document.write
("<h1>" + name + "</h1>")
can write a variable text into an HTML page

32
What can a JavaScript Do?

33
What can a JavaScript Do?
JavaScript variables
can hold:
Numbers: integer and floating point
Booleans: true, false
Strings: "Hello World!"
Objects: myObj = new Object();
Null: empty, no value

34
What can a JavaScript Do?
JavaScript Operators
The usual range of comparison and
arithmetic operators.
String concatenation with +: "Age:" +
"20" gives "Age: 20 "
C style conditional operators:
preferredPet = (cats >
dogs) ? "felines" : "canines"

35
JavaScript
Web servers
Web browser
Request
HTML page
with JavaScript
JavaScript
response

36
JavaScript variables
JavaScript variables
are used to hold values
or the results of expressions.
Declare variables
var
x=5; // after execution x has value 5, or use
x=5
;
var carname="Volvo";
Rules for JavaScript variable names:
Variable names are case sensitive (y and Y are two different variables)
Variable names must begin with a letter or the underscore character followed by
letters, digits or underscore character
Avoid reserve words which have special meanings in the programming
language, like
for
,
var
,
while
and
function
Give memorable and meaningful names, like age, name, subject,
carname etc

;
;

38
JavaScript variables
Special characters – they all begin with a
backslash character (\)
Output Character
Special Code to Use
Backslash (\)
\\
Double quote (")
\"
Single Quote (‘)
\’
Tab
\t


40
JavaScript operators
operators types
Mathematical
Assignment
Comparison
Logical

41
JavaScript operators
Given y=8, the following table explains
mathematical
operators
operator
description
example
Result of x
+
Addition
x=2+3
5
-
Subtraction
x=y-5
3
*
Multiplication
x=y*2
16
/
Division
x=y/2
4
%
Modulus (division
remainder)
x=y%3
2
++
Increment
x=++y
9
--
Decrement
x=--y
1


You've reached the end of your free preview.
Want to read all 96 pages?
- Three '20