Professional Documents
Culture Documents
2
VARIABLES AND VARIABLE
DECLARATIONS
Variables used to store data
Syntax var tenbien;
How to name variables
Variable names include letters and numbers, but cannot
begin with a number
Variable names do not include spaces and punctuation,
except for underscores (_)
Multiple variables can be declared on a single line
var x, y, zeta;
You can declare and initialize a variable at the same
time
var x = 1;
var x = 1, y = "hello“;
3
DATATYPES
4
NUMBER TYPE
5
DEMO
<html>
<head>
<title>Hexadecimal Numbers</title>
<script type="text/javascript">
var h = 0xe;
var i = 0x2;
var j = h * i;
alert(‘Tich la’+j);
</script>
</head>
<body>
<p>Chapter 4 Example</p>
</body>
</html>
6
EX
7
FUNCTIONS THAT WORK WITH
NUMBERS
isNAN()
Determines whether the input parameter is a number
or not
If numeric, return False
If not a number, return True
alert(isNaN("4"))
alert(isNaN("four"))
8
EX
Var x=8;
if(isNaN(x) == false){
alert(x + “is a number ”);
}
else{
alert(x + “is not a number”);
}
9
MATH . UTILITY CLASS
10
CHARACTER STRING TYPE
11
STRING PROPERTIES AND METHODS
12
STRING PROPERTIES AND METHODS
if (x > 18) {
alert("Hi");
}
14
ARRAY
Second way
var convat = new Array("meo", "ho", "voi");
15
EX
16
17
NULL AND UNDEFINED VALUES
Null value
Null means nothing
var x = null; which means x doesn't refer to anything
var x = null; other than var x = "";
Type Undefined
Value not determined yet
var x;
alert(x);
18
LESSON 2: VARIABLES, TYPES,
OPERATORS AND EXPRESSIONS
SQUEEZE STYLE
var x = 100;
alert(“Hello” + x);
20
SQUEEZE STYLE
Explicit pressing
Cast numbers to strings
var x = String(100);
alert(typeof(x));
21
OPERATOR
Arithmetic operators
Toán tử Giải thích
+ Cộng
- Trừ
* Nhân
/ Chia
% Chia lấy dư
Logical operator
Toán tử Giải thích
& Và
| Hoặc
^ XOR
! NOT
22
OPERATOR
Unary operator
23
THE INCREMENT AND DECREMENT
OPERATOR
var x = 4;
x++;
alert (x);
24
THE INCREMENT AND DECREMENT
OPERATOR
Difference between ++x and x++
var x = 4; var x = 4;
var y = ++x; var y = x++;
alert ("x = "+x+" y ="+y); alert ("x = "+x+" y ="+y);
25
FUNCTION PROMPT
Used to get data entered by the user
The function returns the value entered by the
user
The following code will take the data entered by
the user and display the results in the dialog box:
var x = prompt();
alert (x);
Sytax
if (điều_kiện) {
//Thực hiện
}
Cases
if (! điều_kiện) { if (x < 350) { var x = 3;
//Thực hiện //Thực hiện var y = 4;
} } if (x == y) {
//Thực hiện
}
28
EX IF…THEN
29
COMPLEX CONDITIONS
30
Exercise 1: Write a program to find the Max of two numbers
a and b.
Lesson 2: Write a program to find the Max of 3 numbers a, b,
c.
Exercise 3: Write a program to input a student's grades. Print
out the student's academic rating.
(Gradement. If the score is >= 9, Excellent. If the score is from
8 to close to 9, Good. If the score is from 7 to close to 8,
Good. If the score is from 6 to close to 7, Medium, If the score
is from 5 to close to close. 6, Average, the rest is Weak).
Additional requirements: Check that the input score must be
from 0-10, otherwise print the message "You entered invalid
score"
31
32
Thanks you