Vous êtes sur la page 1sur 28

Chapter(2)

Data Types And Variables

Faculty of Information Science 1


Types of Data

1. Numerical Data
2. Text Data
3. Boolean Data

Faculty of Information Science 2


1. Numerical Data

 Whole numbers-such as 145,Integers


(positive or negative), -253 to 253

 Fractional numbers-such as 1.234, floating-


point numbers

Faculty of Information Science 3


2. Text Data

 One or more characters of text is a string


 Literal strings using ' or " characters
For example:
1.“Hello World” or „ Hello World‟
2. “Peter O‟ Toole”
3. „Peter O‟ Toole‟ -error
(\)-backslash character-escape character
4. „Peter O \‟Toole‟
5. „ Hello “Paul” ‟
6. “Hello “Paul””-error
7. “Hello \” Paul \””

Faculty of Information Science 4


Escape Sequences
Escape Sequences Character Represented
\b Backspace
\f Form feed
\n New Line
\r Carriage return
\t Tab
\’ Single quote
\” Double quote
\\ Backslash

\xNN NN is a hexadecimal number that identifies


a character in the Latin-1 character set

Faculty of Information Science 5


3.Boolean Data

 Two values:
 true for yes and
 False for no

Faculty of Information Science 6


Variables

• Variable names are case sensitive.


• For example: myVariable is not the same as
myvariable.
• Can‟t use Reserved words (e.g var or with)
• Can‟t use & and %
• Allow to use numbers, but the name must not begin
with numbers

Faculty of Information Science 7


Variables Names
Invalid names Valid names

With myVariable99

99variables myPercent_Variable

My%Variable the_Good_and_the_Bad

theGood&theBad

Faculty of Information Science 8


Constant, variable
• To define constant
const constant_name = constant value;
• To define variable
var variable_name;
var variable_name1 = initial_value1,
variable_name2 = initial_value2, … ;
Scope of the variable
• Variables can be global (when you declare
outside of any function), or can be local within a
function.
Faculty of Information Science 9
ch2_examp1:Declaring Variables

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<script type="text/javascript">
var myFirstVariable;

myFirstVariable = "Hello";
alert(myFirstVariable); //Hello

myFirstVariable = 54321;
alert(myFirstVariable); //54321

</script>
</body>
</html>

Faculty of Information Science 10


ch2_examp2:Assigning Variables the values of
Other Variables
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script language="JavaScript" type="text/javascript">
var string1 = "Hello";
var string2 = "Goodbye";

alert(string1);//Hello
alert(string2); //Goodbye

string2 = string1;
alert(string1);//Hello
alert(string2); //Hello

string1 = "Now for something different";


alert(string1); // Now for something different
alert(string2);//Hello
</script>
</body>
</html>
Faculty of Information Science 11
ch2_examp3:Calculations
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>

<script language="JavaScript" type="text/javascript">


var firstNumber = 15;
var secondNumber = 10;
var answer;
answer = 15 / 10;
alert(answer); //1.5

alert(15 / 10);//1.5

answer = firstNumber / secondNumber;


alert(answer); //1.5

</script>
</body>
</html> Faculty of Information Science 12
Operator

• Common operators

• Assignment operators(+=, -=, *=, /=, .=)


a = 2; a += 4; // Now a is 6
b = "now"; b += "here"; // Now b is "nowhere"
Faculty of Information Science 13
Operator Precedence

• + and – operators have an equal precedence


• * and / operators also have an equal precedence
 * has a higher precedence than +;
 = (assignment operator) has the lowest precedence
• Works from left to right for equal precedence
operators
• For example:
var myVariable;
myVariable=1+1*2;
Alert(myVariable);//3

Faculty of Information Science 14


ch2_examp4:Fahrenheit to
Centigrade
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script type="text/javascript">
// Equation is °C = 5/9 (°F - 32).
var degFahren = prompt("Enter the degrees in Fahrenheit",50);
var degCent;

degCent = 5/9 * (degFahren - 32);

alert(degCent); //10

</script>
</body>
</html>

Faculty of Information Science 15


String
• string data plays important roles.
• treated as an object.
• string values can be enclosed by either double quotation
or single quotation
a = "ABC"; b = 'ABC';
// a and b are the same
In order to access to individual character within a string,
a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
b = a[0]; // b contains 'A' (the first char)
 length is the only property of the String object
Faculty of Information Science 16
Usage of String
a = "This is a string";
b = a.length;//16 // Length of the string. You can also
write like "Hello".length
c = a.toUpperCase(); // Convert to upper case
characters
d = a.toLowerCase(); // Convert to lower case
characters
d = a.slice(2, 5); // is //Get a part of string (from index 2
to 5 in this case)
e = a.indexOf("is");//2 // Search for a string and returns
the position
f = a.lastIndexOf("s");//10 // Search for a string from the
end of the string
Faculty of Information Science 17
ch2_examp5:Concatenating
Strings
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>

<script type="text/javascript">

var greetingString = "Hello";


var myName = prompt("Please enter your name", "");
var concatString;

document.write(greetingString + " " + myName + "<br>");//Hello name


concatString = greetingString + " " + myName;
document.write(concatString); //Hello name
</script>
</body>
</html>
Faculty of Information Science 18
ch2_examp6:Making the
Temperature Converter User-
Friendly
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script type="text/javascript">
var degFahren = prompt("Enter the degrees in Fahrenheit", 50);
var degCent;

degCent = 5/9 * (degFahren - 32);


alert(degFahren + "\xB0 Fahrenheit is " + degCent + "\xB0 centigrade");
//50˚ Fahrenheit is 10˚ centigrade.
</script>
</body>
</html>

Faculty of Information Science 19


Type casting

 a = parseInt("40 years"); // a will be an integer 40

 b = parseFloat("1.2 times"); // b will be a float 1.2

 c = b.toString(); // c will be a string “1.2”

 E.g a=parseInt(“hello”);//NaN, meaning Not a Number

 isNaN()-which checks whether something is NaN or not

 myVar1 = isNaN(“Hello”);//true

 myVar2 = isNaN(“34”);//false

Faculty of Information Science 20


ch2_examp7:Converting Strings to
Numbers
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script type="text/javascript">

var myString = "56.02 degrees centigrade";


var myInt;
var myFloat;

document.write("\"" + myString + "\" is " + parseInt(myString) + " as an integer" + "<BR>");


// "56.02 degrees centigrade" is 56 as an integer
myInt = parseInt(myString);
document.write("\"" + myString + "\" when converted to an integer equals " + myInt + "<BR>");
// "56.02 degrees centigrade" when converted to an integer equals 56
myFloat = parseFloat(myString);
document.write("\"" + myString + "\" when converted to a floating point number equals " + myFloat);
// "56.02 degrees centigrade" when converted to a floating point number equals 56.02
</script>
</body>
</html>

Faculty of Information Science 21


Array
• arrays are also treated as objects
• var a = new Array(); // Declares a new array
with no data
• var b = new Array("One", "Two", 3);// Array
with 3 elements
• b[3] = "Four"; // You can add new element to an
existing array
• b[5] = "Six"; // You can skip the index, but the
skipped element is also created
Faculty of Information Science 22
Usage of Array
var a = [1, 2, 3, 4, 5];
b = a.length; // Number of elements in the array.
Returns 5 in this case.
c = a.concat(array2); // Join 2 arrays into one array
d = a.pop(); // Removes the last element from an
array and returns that element.
a.push(); // Add one or more elements to the array
and returns the array
a.sort(); // Sort the array elements

Faculty of Information Science 23


ch2_examp8:An Array
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script type="text/javascript">

var myArray = new Array();


myArray[0] = "Bob";
myArray[1] = "Pete";
myArray[2] = "Paul";

document.write("myArray[0] = " + myArray[0] + "<BR>");//myArray[0] = Bob


document.write("myArray[2] = " + myArray[2] + "<BR>");//myArray[2] = Paul
document.write("myArray[1] = " + myArray[1] + "<BR>"); //myArray[1] = Pete

myArray[1] = "Mike";
document.write("myArray[1] changed to " + myArray[1]); //myArray[1] changed to Mike

</script>
</body>
</html>

Faculty of Information Science 24


ch2_examp9:A Two-Dimensional
Array
<html xmlns=http://www.w3.org/1999/xhtml><body>
<script type="text/javascript">
var personnel = new Array();

personnel[0] = new Array();


personnel[0][0] = "Name0";
personnel[0][1] = "Age0";
personnel[0][2] = "Address0";

personnel[1] = new Array();


personnel[1][0] = "Name1";
personnel[1][1] = "Age1";
personnel[1][2] = "Address1";

personnel[2] = new Array();


personnel[2][0] = "Name2";
personnel[2][1] = "Age2";
personnel[2][2] = "Address2";
document.write("Name : " + personnel[1][0] + "<BR>"); //Name : Name1
document.write("Age : " + personnel[1][1] + "<BR>");//Age : Age1
document.write("Address : " + personnel[1][2]);// Address : Address1
Faculty of Information Science 25
</script> </body> </html>
Exercise questions 

1. Write a JavaScript program to convert degrees


centigrade into degrees Fahrenheit, and to
write the result to the page in a descriptive
sentence. The JavaScript equation for
Fahrenheit to centigrade is as follows:
degFahren= 9 /5 * degCent + 32

Faculty of Information Science 26


Exercise questions
2. The following code use the prompt() function to get two numbers from the user. It then adds
those two numbers together and writes the result to the page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script type="text/javascript">

var firstNumber = prompt("Enter the first number","");


var secondNumber = prompt("Enter the second number","");

var theTotal = firstNumber + secondNumber;

document.write(firstNumber + " added to " + secondNumber + " equals " + theTotal);


</script>
</body>
</html>

Faculty of Information Science 27


Faculty of Information Science 28

Vous aimerez peut-être aussi