Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Basic Data Types Numbers (integer and floating point)‏ Strings (sequences of characters)‏ Boolean values (true/false)‏"— Presentation transcript:

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

2 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)‏

3 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

4 String literals any sequence of characters enclosed in a matching pair of single quotes (') or double quotes (")‏ be careful with quote characters inside strings

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

6 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

7 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

8 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'

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

10 Compound Statement: if Composed of a condition and a sequence of statements if true and sequence of statements if false if ( income > 50000 ) then { tax = income * 0.25; } else { tax = income * 0.18; }

11 Compound Statement: while Composed of a condition and a sequence of statements that are executed repeatedly until the condition is false while ( X > 0 ) { Y = Y + X; X = X – 3; }

12 Other Selection/Looping Constructs switch do / while for break/continue

13 Switch switch (command)‏ { case "Fetch!": … break; case "Roll over!": … break; default: … }

14 do/while do { … } while ( a < b );

15 for for ( count = 0; count < 10; count++ )‏ { x = count*x; … }

16 break/continue break – used to exit a loop at an abnormal location continue – used to return to the top of the loop prematurely

17 Declaring functions function difference( a, b ) { var diff; if ( a < b ) { diff = b – a; } else { diff = a – b; } return diff; }

18 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

19 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

20 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

21 Scoping Tragedy #1 function foo( a, b ) { foo = a * b; return foo; } alert( foo( 2, 2 ) ); alert( foo( 3, 5 ) );

22 Empty Statements var A = 7; while ( A > 0 ); { alert( A*A ); A = A – 1; }

23 Implicit Type Conversion while( first_guess – next_guess )‏ { … } be careful using floating point values where a boolean value is required

24 Object An object is a collection of named pieces of data These named values are usually referred to as properties of the object

25 Creating objects #1 var emp1 = {}; emp1.name = "Bob"; emp1.age = 55; emp1.yearsW = 20;

26 Creating objects #2 var emp2 = { name:"Carol", age:43, yearsW:15 };

27 Properties can be: primitive data values (Number, String, Boolean)‏ other objects functions; in this case, the property is called a method

28 Example var club = {}; club.name = "Heck's Angles"; club.secretary = emp1; club.treasurer = emp2; alert ( club.secretary.name ); alert ( club.treasurer.yearsW );

29 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

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

31 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

32 Creating arrays #1 var foods = []; foods[0] = "apple"; foods[1] = "pear"; foods[2] = "banana";

33 Creating arrays #2 var cars = [ "Ford", "Honda", "BMW" ];

34 Array elements can be: primitive data values (Number, String, Boolean)‏ objects other arrays functions

35 Example var club_members = []; club_members[0] = emp1; club_members[1] = emp2; // "Bob" alert ( club_members[0].name ); // 15 alert ( club_members[1].yearsW );

36 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

37 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

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

39 Running JavaScript Embed the script in an XHTML page: The file named by the src attribute contains your JavaScript program Reload the XHTML page in the browser

40 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

41 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

42 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

43 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


Download ppt "Basic Data Types Numbers (integer and floating point)‏ Strings (sequences of characters)‏ Boolean values (true/false)‏"

Similar presentations


Ads by Google