Week4 JavaScript Lecture
Week4 JavaScript Lecture
Outline – Part A
Overview of JavaScript
Versions, embedding, comments
JavaScript Basics
Variables and Data Types
Operators
Expressions
Error in JavaScript
Exception Handling in JavaScript
Outline – Part C
Working with Browser Objects
Document Object Model (DOM)
Creating Cookies in JavaScript
Constructing a standard cookie
Interaction with the cookie
Introducing DHTML
Styles
and Layers
Dynamic Positioning
Outline – Part D
JavaScript Application Development
JavaScript Example
Discuss the execution requirements
<html>
<head>
<title>First JavaScript Program</title>
</head>
<body>
<script language=“JavaScript” src=“your_source_file.js”></script>
</body>
</html>
<script language=“JavaScript”
src=“your_source_file.js”></script>
<script language="JavaScript">
alert("This is an Alert method");
confirm("Are you OK?");
prompt("What is your name?");
prompt("How old are you?","20");
</script>
Variables
JavaScript allows you to declare and use variables to
store values.
How to assign a name to a variable?
Include uppercase and lowercase letters
Digits from 0 through 9
The underscore _ and the dollar sign $
No space and punctuation characters
First character must be alphabetic letter or underscore
Case-sensitive
No reserved words or keywords
Which one is legal?
My_variable
Legal
$my_variable
1my_example
_my_variable
@my_variable
My_variable_example
++my_variable
%my_variable Illegal
#my_variable
~my_variable
myVariableExample
Variable on-the-fly
<head>
<script language=“JavaScript”>
var id;
Variable declaration
id = prompt(“What is your student id number?”);
alert(id);
name = prompt(“What is your name?”,”No name”);
alert(name);
</script>
</head>
(1*10 + 0*7) = 10
Strings
A string variable can store a sequence of
alphanumeric characters, spaces and special
characters.
String can also be enclosed in single quotation
marks (‘) or in double quotation marks (“).
What is the data type of “100”?
String but not number type
Strings example
<head> <script language=“JavaScript”>
document.write("This is a string."+"<BR>");
document.write("This string contains 'quote'.");
var myString = "alert string";
alert(myString); </script> </head>
Unlike Java and C, JavaScript does not have a single character (char)
data type.
typeof operator
<head> <script language=“JavaScript”>
var x = “hello”, y;
alert(“Variable x value is “ + typeof(x));
alert(“Variable y value is “ + typeof(y));
alert(“Variable x value is “ + typeof(z));
</script> </head>
It is an unary operator.
Return either: Number, string, Boolean, object, function, undefined, null
What is an Object?
<html>
<head>
<title> Null and Undefined example </title>
<script language=“JavaScript”>
var test1, test2 = null;
alert(“No value assigned to the variable” + test1);
alert(“A null value was assigned” + test2);
</script>
</head>
<body> … </body>
</html>
JavaScript Special Characters
Character Meaning
\b Backspace
\f Form feed
\t Horizontal tab
\n New line
\r Carriage return
\\ Backslash
\’ Single quote
\” Double quote
Expressions
It is a set of literals, variables, operators that merge
and evaluate to a single value.
Left_operand operator right_operand
By using different operators, you can create the
following expressions.
Arithmetic,logical
String and conditional expressions.
Operators
Arithmetic operators
Logical operators
Comparison operators
String operators
Bit-wise operators
Assignment operators
Conditional operators
Arithmetic operators
Name Example
Post Incrementing operator Counter++
Post Decrementing operator Counter--
Pre Incrementing operator ++counter
Pre Decrementing operator --counter
Logical operators
< Less than “true” if left operand is less than right operand 3<5
>= Greater than or “true” if left operand is greater than or equal to the 5 >= 2
equal right operand
<= Less than or “true” if left operand is less than or equal to the 5 <= 2
equal right operand
Strict Equality Operators
<script language=“JavaScript”>
var currentWord=“75”;
var currentValue=75;
var outcome1=(currentWord == currentValue);
var outcome2=(currentWord === currentValue);
alert(“outcome1: “ + outcome1 + “ : outcome2: “ + outcome2);
</script>
<script language=“JavaScript”>
if (alpha = beta) { … }
if (alpha == beta) { … }
</script>
“if” statement
“if … else” statement
“switch” statement
“if” statement
if (condition) { statements; }
It is the main conditional statement in JavaScript.
The keyword “if” always appears in lowercase.
The condition yields a logical true or false value.
The condition is true, statements are executed.
“if” statement example
<script language=“JavaScript”>
var chr = “”;
…
if (chr == ‘A’ || chr == ‘O’) {
document.write(“Vowel variable”);
}
</script>
“if … else” statement
if (condition) { statements; }
else { statements; }
if (condition) { statement; }
else if (condition) { statement; }
else { statement; }
Allows you to test for multiple expression for
one true value and executes a particular block
of code.
“if/if…else” statement example
<script language=“JavaScript”>
var chr;
chr = prompt(“Please enter a character : “,””);
if (chr >= ‘A’){
if (chr <= ‘Z’)
alert(“Uppercase”);
else if (chr >= ‘a’){
alert(“Lowercase”);
}
}
</script>
“switch” statement
switch (expression) {
case label1:
statements; break;
default:
statements;
}
<script language=“JavaScript”>
var chr;
chr = prompt(“Pls enter a character in lowercase:”,””);
switch(chr){
case ‘a’ :
alert(“Vowel a”); break;
case ‘e’ :
alert(“Vowel e”); break;
default :
alert(“Not a vowel”);
}
</script>
Looping Statement
“for” Loops
“for/in” Loops
“while” Loops
“do … while” Loops
“break” statement
“continue” statement
“for” statement
<script language=“JavaScript”>
var counter;
for (counter = 1; counter <= 10; counter++)
{
document.write(counter*counter + “ “);
}
</script>
<script language=“JavaScript”>
var book; (What is the difference if “var book=“”;)
var booklist = new Array(“Chinese”, “English”, “Jap”);
for (var counter in booklist) {
book += booklist[counter] + “ “;
}
alert(book);
</script>
“while” statement
<html>
<head>
<title>While loop example</title>
<script language=“JavaScript”>
var counter = 100;
var numberlist = “”;
while (counter > 0) {
numberlist += “Number “ + counter + “<br>”;
counter -= 10;
}
document.write(numberlist);
</script> <body> … </body>
</html>
“do … while” statement
do {
statements;
counter increment/decrement;
} while (termination condition)