CIS 179 – Web Script Programming

Lecture Notes
 

Tutorial 3 - Data Types and Operators

Escape Sequence Character

\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\’ Single quotation mark
\" Double quotation mark
\\ Backslash

array_name = new Array(number of elements); animals = new Array(5); // creates an array of 5 elements
animals[0] = "dog"; // first element
animals[1] = "cat"; // second element
animals[2] = "rat"; // third element
animals[3] = "elephant"; // fourth element
animals[4] = "unicorn"; // fifth element
animals = new Array("dog", "cat", "horse"); // 3 elements + Add
- Subtract
* Multiply
/ Divide
% Modulus- Divides two integer operands and returns the remainder.
 
Example:
var x, y, sum;
x = 100;
y = 200;
sum = x + y;
++operand Pre-increment
operand++ Post-increment
--operand Pre-decrement
operand-- Post-decrement
-operand Negation
var count = 10;
var newValue = ++count * 2; // newValue = 22, count = 11
var count = 10;
var newValue = count++ * 2; // newValue = 20, count = 11
operand = expression Assigns the result of expression to operand
operand += expression Equivalent to operand = operand + expression
operand -= expression Equivalent to operand = operand - expression
operand *= expression Equivalent to operand = operand * expression
operand /= expression Equivalent to operand = operand / expression
operand %= expression Equivalent to operand = operand % expression
 
 
var x, y;
x = "Hello ";
x += "World"; // x changes to "Hello World"

x = 100;
y = 200;
x += y; // x changes to 300

x = 3;
y = 2;
x %= y; // x changes to 1 (remainder of 3/2)

= = Returns true if the operands are equal
!= Returns true if the operand are not equal
> Returns true if the left operand is greater than the right operand
< Returns true if the left operand is less than the right operand
>= Returns true if the left operand is greater than or equal to the right operand
<= Returns true if the left operand is than or equal to the right operand
&& And operator
operand1 operand2 result
false    false    false
false    true     false
true     false    false
true     true     true

|| Or operator
operand1 operand2 result
false    false    false
false    true     true
true     false    true
true     true     true

! Not operator
operand result
false   true
true    false
 
 

var a = 2; var b = 3;
var returnValue = (a == 2) && (b == 3); // returns true
var firstString = "Arthur C. Clarke wrote ";
var newString;
newString = firstString + "<I>2001 A Space Odyssey</I>";  
OR
 
var firstString = "Arthur C. Clarke wrote ";
firstString += "<I>2001 A Space Odyssey</I>";
 
() []                 Highest precedence
! - ++ -- typeof void
* / %
+ -
< <= > >=
== !=
&&
||
= += -= *= /= %=      Lowest precedence
Assignment: Do exercise 4 on page 144. Include a text box for temperature input and two buttons, one button for converting to Celsius and one button for converting to Fahrenheit. Display the answer in a second text box.