S? D?ng Bi?n Trong Javascript

M?t trong nh?ng kh�c bi?t gi?a ng�n ng? HTML v� ng�n ng? l?p tr�nh nh? Javascript l� vi?c s? d?ng bi?n (Variables-Vi?t t?t l� var). Trong ch??ng tr�nh Javascript s? d?ng bi?n ?? l?u tr? th�ng tin.

Javascript cho ph�p ch�ng ta l?u tr? v� v?n d?ng d? li?u trong ch??ng tr�nh. V� d?: x=2; l� g�n gi� tr? 2 cho bi?n x.

V� d? 1:

Vi?t gi� tr? bi?n:
Cho x=2, d�ng javascript vi?t ra gi� tr? c?a x: x=2

Select v� Copy Code :


<html>
<head>
<title>Vi?t gi� tr? bi?n
<body>
Cho x=2, d�ng javascript vi?t ra gi� tr? c?a x: x=2

<script language="Javascript">
<!--
var
x=2;

document.write("Gi� tr? c?a x l�: ", x);
//-->
</script>
</body>
</html>


Khi ch??ng tr�nh tr�n ???c th?c hi?n, tr�n browser ta s? th?y c�u: Gi� tr? c?a x l� 2

V� d? 2: B�i T�an C?ng Cho 2 bi?n x=2; v� y=3; vi?t ra gi� tr? x+y;


Select v� Copy Code :


<html>
<head>
<title>Simple Arithmetic</title>
<body>
Javascript l�m t�an ??n gi?n: <br>
<script language="Javascript">
var
x=2;
y=3;
document.write(x, "+", y , "=", x+y);
</script>
</body>
</html>


K?t qu? th?y tr�n browser khi ch?y ch??ng tr�nh: 2+3=5


V� d? 3: Gi� tr? bi?n l� m?t chu?i k� t?.

Cho bi?n Name="New Visitor"; Vi?t ra c�u: Hello New Visitor


Select v� Copy Code :


<html>
<head>
<title>Hello New visitor</title>
<body>
Vi?t c�u ch�o <br>
<script language="Javascript">
var
Name="New Visitor";
document.write("Hello ", Name);
</script>
</body>
</html>

K?t qu? th?y tr�n browser khi ch?y ch??ng tr�nh l� : Hello New Visitor.


V� d? 4: Gi� tr? bi?n ???c n?p v�o t? ng??i s? d?ng.

Y�u c?u: Khi ch?y ch??ng tr�nh, c?a s? Prompt m? ra, visitor n?p v�o t�n, vi?t ra c�u:
Welcom, + "T�n c?a visitor" v�o ngay v? tr� ??t ??an codes.



Select v� Copy Code :


<html>
<head>
<title>Hello visitor</title>
<body>
<script language="Javascript">
var
Username=window.prompt("Enter your name");
document.write("Welcome, ", Username);
</script>
</body>
</html>


K?t qu? th?y tr�n browser khi ch?y ch??ng tr�nh l� : Welcome +T�n c?a visitor n?p v�o.

This entry was posted on 21:50 and is filed under , .Follow any comments here with the RSS 2.0 . You can leave a response so that we can know your suggestion.
0 Responses to "S? D?ng Bi?n Trong Javascript"

Đăng nhận xét