By GokiSoft.com|
10:01 18/01/2021|
Học JS
[Share Code] Tìm hiểu Javascript biến, toán tử, hàm căn bản, function - Lập trình HTML/CSS/JS
Kien thuc JS
- JS Core:
- khai bao bien, toan tu, bieu thuc logic
- Menh de dieu kien (if, else, switch)
- Loop: while, for, do .. while
- Function
- Object & Array
- JS advanced
- Event
- Tuong tac len tags html (doc noi dung tag, thay doi noi dung tags, .v.v)
- LocalStorage: Luu tru du lieu (Cookie, Session, )
<!DOCTYPE html>
<html>
<head>
<title>Javascript tutorial</title>
<meta charset="utf-8">
</head>
<body>
<h1 id="title">Welcome to learn Javascript</h1>
<button onclick="clickX()">Nhap x</button>
<button onclick="clickY()">Nhap y</button>
<button onclick="clickKQ()">In Ket Qua</button>
<script type="text/javascript">
//code js
// var font = 0;
// setInterval(function() {
// var titleTag = document.getElementById('title')
// titleTag.style.fontSize = ++font + "px"
// var a = Math.random() * 255
// var b = Math.random() * 255
// var c = Math.random() * 255
// titleTag.style.color = `rgb(${a}, ${b}, ${c})`
// }, 100)
// Khai bao bien
var a = 10 //a => so nguyen
var b = "Xin chao" //b => String, text, mang ky tu
a = "Good morning" //a => Hieu la String, text, mang ky tu
a = 'Xin chao' //a => String, text, mang ky tu
// document.write(a) //Su dung test => Trong lap trinh => khong dung
console.log(a)
x = "TRAN VAN A"
console.log(x)
// Toan tu, +, -, *, /, %
var x = 5
var y = 6
var z = x + y
console.log(z)
var t = "6"
k = x + t
console.log(k)
k = x - t
console.log(k)
k = x * t
console.log(k)
k = x / t
console.log(k)
//Tinh tong cua x + t => 11
k = x + t*1
console.log(k)
k = x + parseInt(t)
console.log(k)
// Mot so function can ban
// alert('May ban da bi nhieu virus')
// option = confirm('Ban co muon xoa thong tin tai khoan nay khong???')
// console.log(option)
// code = 'var t1 = 5; t2 = ++t1; console.log(t2)'
// console.log(code)
// eval(code)
// x = prompt('Nhap gia tri x')
// y = prompt('Nhap gia tri y')
// t = parseInt(x) + parseInt(y)
// console.log(t)
// x = 5
// y = ++x
// console.log(y)
// console.log(x)
// t = (x==5)?"Gia tri 5":"Gia tri khac 5"
// console.log(t)
// var quantity = prompt('Enter quantity of product:',0);
// if(quantity < 0 || isNaN(quantity))
// {
// alert('Please enter a positive number.');
// }
// var firstNumber = prompt('Enter first number:',0);
// var secondNumber = prompt('Enter second number',0);
// var result = 0;
// if (secondNumber == 0)
// {
// alert('ERROR Message: Cannot divide by zero.');
// }
// else
// {
// result = firstNumber/secondNumber;
// alert('Result: ' + result);
// }
var t = 5;
switch(t) {
case 1:
console.log('Xin chao')
break;
case 5:
console.log('Abc...')
break;
default:
console.log('khac...')
break;
}
function showAlert() {
alert('Xin chao...')
}
// showAlert()
function tinhtong(n) {
var sum = 0
for (var i = 1; i <= n; i++) {
sum += i
}
console.log(sum)
}
tinhtong(10)
tinhtong(4)
function cong(x, y) {
var sum = x + y
console.log(sum)
}
cong(2, 6)
function tru(x, y) {
return x - y
}
var kq = tru(6, 2)
console.log('Phep tru: ' + kq)
var x, y;
function clickX() {
x = prompt('Nhap x')
console.log("x: " + x)
}
function clickKQ() {
kq = parseInt(x) + parseInt(y)
console.log("kq: " + kq)
}
function clickY() {
y = prompt('Nhap y')
console.log("y: " + y)
}
</script>
</body>
</html>
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)