Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean...

43
Basic Data Types • Numbers (integer and floating point) • Strings (sequences of characters) • Boolean values (true/false)

Transcript of Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean...

Page 1: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Basic Data Types

• Numbers (integer and floating point)

• Strings (sequences of characters)

• Boolean values (true/false)

Page 2: 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)

Page 3: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 4: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

String literals

• any sequence of characters enclosed in a matching pair of single quotes (') or double quotes (")

• be careful with quote characters inside strings

Page 5: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Examples

• 'a string literal #@!? 1234'• ' he said, "Stop!" '• " he said, 'Stop!' "• ' what's that sound' - ERROR• ' what\'s that sound' - OK

Page 6: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 7: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 8: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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'

Page 9: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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;

Page 10: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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;

}

Page 11: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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;

}

Page 12: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Other Selection/Looping Constructs

• switch

• do / while

• for

• break/continue

Page 13: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Switch

switch (command){case "Fetch!":

…break;

case "Roll over!":…break;

default:…

}

Page 14: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

do/while

do

{

}

while ( a < b );

Page 15: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

for

for ( count = 0; count < 10; count++ )

{

x = count*x;

}

Page 16: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

break/continue

• break – used to exit a loop at an abnormal location

• continue – used to return to the top of the loop prematurely

Page 17: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Declaring functions

function difference( a, b ) {

var diff;

if ( a < b ) { diff = b – a; }

else { diff = a – b; }

return diff;

}

Page 18: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 19: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 20: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 21: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Scoping Tragedy #1

function foo( a, b ) {

foo = a * b;

return foo;

}

alert( foo( 2, 2 ) );

alert( foo( 3, 5 ) );

Page 22: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Empty Statements

var A = 7;

while ( A > 0 );

{

alert( A*A );

A = A – 1;

}

Page 23: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Implicit Type Conversion

while( first_guess – next_guess )

{

}

• be careful using floating point values where a boolean value is required

Page 24: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Object

• An object is a collection of named pieces of data

• These named values are usually referred to as properties of the object

Page 25: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Creating objects #1

var emp1 = {};

emp1.name = "Bob";

emp1.age = 55;

emp1.yearsW = 20;

Page 26: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Creating objects #2

var emp2 = { name:"Carol",

age:43,

yearsW:15 };

Page 27: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Properties can be:

• primitive data values (Number, String, Boolean)

• other objects

• functions; in this case, the property is called a method

Page 28: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Example

var club = {};

club.name = "Heck's Angles";

club.secretary = emp1;

club.treasurer = emp2;

alert ( club.secretary.name );

alert ( club.treasurer.yearsW );

Page 29: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 30: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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 );

Page 31: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 32: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Creating arrays #1

var foods = [];

foods[0] = "apple";

foods[1] = "pear";

foods[2] = "banana";

Page 33: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Creating arrays #2

var cars = [ "Ford",

"Honda",

"BMW" ];

Page 34: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Array elements can be:

• primitive data values (Number, String, Boolean)

• objects

• other arrays

• functions

Page 35: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

Example

var club_members = [];

club_members[0] = emp1;

club_members[1] = emp2;

// "Bob"

alert ( club_members[0].name );

// 15

alert ( club_members[1].yearsW );

Page 36: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 37: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 38: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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] );

Page 39: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 40: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 41: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 42: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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

Page 43: Basic Data Types Numbers (integer and floating point) Strings (sequences of characters) Boolean values (true/false)

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