* 6&%(,#.(#'#11#27 - courses.cs.washington.edu...Review List !"#$ List
Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean...
-
Upload
doreen-gibson -
Category
Documents
-
view
226 -
download
1
Transcript of Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean...
Basic Data Types
• Numbers (integer and floating point)
• Strings (sequences of characters)
• Boolean values (true/false)
Integer literals
• decimal: any sequence of decimal digits not starting with 0
• octal: a 0 followed by any sequence of octal digits
• hexadecimal: a 0x (or 0X) followed by any sequence of hexadecimal digits (0-9,a-f,A-F)
Floating point literals
• decimal integer followed by:
• optionally, the decimal point (.) and a sequence of decimal digits followed by:
• optionally, an exponent consisting of the letter ‘e’ or ‘E’, optionally, the sign of the exponent (+|-) and the decimal integer value of the exponent
String literals
• any sequence of characters enclosed in a matching pair of single quotes (') or double quotes (")
• be careful with quote characters inside strings
Examples
• 'a string literal #@!? 1234'• ' he said, "Stop!" '• " he said, 'Stop!' "• ' what's that sound' - ERROR• ' what\'s that sound' - OK
Implicit Type Conversions
truestring as number
non-empty string
Context
1 / 0"true"/
"false"
true/false
0 – false
other - true
number as string
number
false0empty string
BooleanNumberString
value
Variables
• an area of memory allocated to hold numeric, string, or boolean value
• The value of a variable can "vary" or change as a program executes
• In JavaScript, even the type of the variable can change – JavaScript is loosely typed
• In other languages, the type of a variable is not allowed to change – strongly typed
String operators
• the plus (+) operator can be used to concatenate two strings
var x = " house ";
var y = ' cat ';
z = x + y; // ' house cat '
z = x + 'boat'; // ' house boat'
z = "large" + y; // 'large cat'
Combining Strings and Numbers
• the plus (+) can be used to combine strings and numbers resulting in a string; but be careful!
var x = 12; var y = 7;var z = x + " is the answer";z = "the answer is " + y;z = x + y + " is the answer";z = "The answer is " + x + y;
Compound Statement: if
• Composed of a condition and a sequence of statements if true and sequence of statements if falseif ( income > 50000 ) then {
tax = income * 0.25;} else {
tax = income * 0.18;
}
Compound Statement: while
• Composed of a condition and a sequence of statements that are executed repeatedly until the condition is falsewhile ( X > 0 ) {
Y = Y + X;X = X – 3;
}
Other Selection/Looping Constructs
• switch
• do / while
• for
• break/continue
Switch
switch (command){case "Fetch!":
…break;
case "Roll over!":…break;
default:…
}
do/while
do
{
…
}
while ( a < b );
for
for ( count = 0; count < 10; count++ )
{
x = count*x;
…
}
break/continue
• break – used to exit a loop at an abnormal location
• continue – used to return to the top of the loop prematurely
Declaring functions
function difference( a, b ) {
var diff;
if ( a < b ) { diff = b – a; }
else { diff = a – b; }
return diff;
}
Variable Scoping
• every variable in a program either has global or local scope
• global scope means that the variable can be referred to anywhere within the entire program
• local scope means the the variable can be referred to only within the function it is declared
• local declarations of variables "hide" global variables with the same name
Variable Scoping
var A = 3;function f() { var A = 7;}f();alert( A );
function f() { var A = 7;}f();alert( A );// error!
var A = 3;function f() { A = 7;}f();alert( A );
Hidden ALocal AGlobal A
Variable Scoping Guidelines
• there should be as few global variables as possible in a program; none is ideal
• functions should depend on as few global variables as possible; none is ideal
Scoping Tragedy #1
function foo( a, b ) {
foo = a * b;
return foo;
}
alert( foo( 2, 2 ) );
alert( foo( 3, 5 ) );
Empty Statements
var A = 7;
while ( A > 0 );
{
alert( A*A );
A = A – 1;
}
Implicit Type Conversion
while( first_guess – next_guess )
{
…
}
• be careful using floating point values where a boolean value is required
Object
• An object is a collection of named pieces of data
• These named values are usually referred to as properties of the object
Creating objects #1
var emp1 = {};
emp1.name = "Bob";
emp1.age = 55;
emp1.yearsW = 20;
Creating objects #2
var emp2 = { name:"Carol",
age:43,
yearsW:15 };
Properties can be:
• primitive data values (Number, String, Boolean)
• other objects
• functions; in this case, the property is called a method
Example
var club = {};
club.name = "Heck's Angles";
club.secretary = emp1;
club.treasurer = emp2;
alert ( club.secretary.name );
alert ( club.treasurer.yearsW );
Object arguments to functions
• primitive data values are passed into functions by-value; a copy of the argument is made inside the function
• object data values are passed into functions by-reference; the function and the caller refer to the same data values
Call-by- Value vs. Reference
var emp = { name:"john",
age:27 };
function foo( x ) {
x.name = "bob";}
foo( emp);
var emp = { name:"john",
age:27 };
function foo( x ) {
x = "bob";}
foo( emp.name );
Arrays
• An array is a collection of data values similar to an object
• In this case, each value in the array is referred to by a number which is its index
• The indexes of an array start with 0
• Usually the values within an array are homogeneous
Creating arrays #1
var foods = [];
foods[0] = "apple";
foods[1] = "pear";
foods[2] = "banana";
Creating arrays #2
var cars = [ "Ford",
"Honda",
"BMW" ];
Array elements can be:
• primitive data values (Number, String, Boolean)
• objects
• other arrays
• functions
Example
var club_members = [];
club_members[0] = emp1;
club_members[1] = emp2;
// "Bob"
alert ( club_members[0].name );
// 15
alert ( club_members[1].yearsW );
Array length "property"
• every array has a "property" length which is the number of elements in the array
• e.g.var cars = [ "Ford",
"Honda",
"BMW" ];
cars.length; // 3
Value vs. Reference Types
• Numbers, Strings, and Booleans are value types; a copy is made when these values are assigned or passed to a function
• Objects and Arrays are reference types, only a copy of the reference is made when these values are assigned or passed to function; the entire object/array is not duplicated
Call-by- Value vs. Reference
var food = [ "lemon",
"orange" ];
function foo( x ) {
x[0] = "bob";}
foo( food );
var food = [ "lemon",
"orange" ];
function foo( x ) {
x = "bob";}
foo( food[0] );
Running JavaScript
• Embed the script in an XHTML page:
<script type="text/javascript" src="gcd.js"> </script>
• The file named by the src attribute contains your JavaScript program
• Reload the XHTML page in the browser
Built-in functions
• alert( message ) – displays the string message in a dialogue box
• prompt( message, default) – requests input from the user; message is displayed and default is the default value
Built-in functions
• parseInt( string, base ) – converts the string argument to an integer value assuming it is in base base
• parseFloat( string ) – converts the string argument to a floating point value
Math functions
• Math.abs(x) – absolute value of x• Math.sqrt(x) – square root of x• Math.min(a,b) – minimum of a, b• Math.max(a,b) – maximum of a, b• Math.floor( x ) – x rounded down to nearest int• Math.ceil( x ) – x rounded up to nearest int• Math.round( x) – usual rounding rules
Debugging JavaScript
• Use the Netscape browser
• if the program fails to run type:
javascript:
in the "Location:" field in the browser
• Correct the errors displayed
• reload the XHTML document
• repeat