You are on page 1of 33

LESSON 2: VARIABLES, TYPES,

OPERATORS AND EXPRESSIONS


CONTENT

 Variables and variable declarations


 Datatypes
 Squeeze style
 Operator
 Control structure
 Single selection command

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

 JavaScript supports the following data types


 Number type
 Character string type
 Boolean style
 Array type
 Object

4
NUMBER TYPE

 JavaScript does not split into Double, Integer...


like other languages.
 Java lumps together into a single numeric type
 var 20
 var 1.5
 var 0xd

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

Example: Declare 2 numbers and output the sum,


difference and average of 2 numbers in JS

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

Example: Use isNaN(x) function to check if x is a


number (true result means x is not a number)

Var x=8;
if(isNaN(x) == false){
alert(x + “is a number ”);
}
else{
alert(x + “is not a number”);
}

9
MATH . UTILITY CLASS

 Javascript provides the Math class, which


includes methods for manipulating numeric data
Phương thức Giải thích Ví dụ Kết quả
Math.random() Trả về số ngẫu nhiên trong
khoảng từ 0 đến 1

Math.abs(x) Trả về giá trị tuyệt đối của x Math.abs(-2) 2

Math.pow(x,y) Trả về giá trị của x mũ y Math.pow(2,3) 8


Math.round(x) Trả về giá trị làm tròn của x Math.round(2.6) 3

10
CHARACTER STRING TYPE

 String surrounded by double quotes


 "Hello world"
 Special characters
Ký tự Giải thích
\' '
\" "
\b Dấu cách
\t Dấu tab
\n Xuống dòng
 alert("Hello\t\'hi\'\ngoodbye!")

11
STRING PROPERTIES AND METHODS

 The length . property


 var x = "I'm String."; alert(x.length);

 substring method (get substring)var x = "Toi la


String."; alert(x.subString(0,3));

12
STRING PROPERTIES AND METHODS

 concat method (string concatenation)


var firstString = " Day la mot xau ky tu “;
var finalString = firstString.concat(" Them mot xau ky tu nua ");
alert(finalString);

 toUpperCase method(uppercase string)


var firstString = " Day la mot xau ky tu “;
var finalString = firstString.toUpperCase();
alert(finalString);

 toLowerCase method (lowercase)


var firstString = " Day la mot xau ky tu “;
var finalString = firstString.toLowerCase();
alert(finalString);
13
TYPE BOOLEAN

 Boolean type has two values, true and false


 Boolean expressions are commonly used in
control structures

if (x > 18) {
alert("Hi");
}

14
ARRAY

 An array is a data type used to store a set of data


of the same type
 Syntax for declaring and assigning values ​to
arrays
 The first way
var convat = new Array();
convat[0] = "meo";
convat[1]= "ho";
convat[2] = "voi";

 Second way
var convat = new Array("meo", "ho", "voi");
15
EX

 Example1 : Enter an array of 5 elements 1->5


prints out the values ​in the array
Var A= new array(1,2,3,4,5);
alert(A[0]); alert(A[1]); alert(A[2]);
alert(A[3]); alert(A[4]);

var array =[];


for (var i = 0; i <5;i++) {
array[i]= prompt("nhập vào giá trị: "); }
alert("Các giá trị trong mãng là: \n" + array);

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

 Implicit type casting


 The interpreter automatically converts the type

var x = 100;
alert(“Hello” + x);

20
SQUEEZE STYLE

 Explicit pressing
 Cast numbers to strings

var x = String(100);
alert(typeof(x));

 Casting string to number


var x = “100”;
var y = Number(x);
alert(typeof(y));

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

 Relational operator and equals operator


Toán tử Giải thích
> Lớn hơn
< Bé hơn
>= Lớn hơn hoặc bằng
<= Bé hơn hoặc bằng
== Bằng
!= Khác

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

Cửa sổ cho người dùng


nhập thông tin Hiển thị giá trị người
26
dùng đã nhập
27
SINGLE SELECTION COMMAND

 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

var x = prompt("Hay nhap vao so be hon 100:");


if (x >= 100)
{
alert("Ban vua nhap gia tri la: " + x + ", gia tri nay khong
be hon 100");
}

29
COMPLEX CONDITIONS

 Use logical operators to combine multiple simple


clauses into a more complex one
var x = prompt("Hay nhap vao so be hon 100 va lon hon 50:");

if ((x < 51) || (x > 99)) {


document.write("<h1>Ban vua nhap gia tri la: " + x + </h1>");
document.write("<h2>Gia tri nay khong nam trong” +
” khoang tu 50 den 100</h2>");
}

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

You might also like