工具:eclipse
第一例
js算数运算符
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js算数运算符</title>
<script type="text/javascript">
var x = 10, y = 4;
document.write("x=" + x + "," + "y=" + y + "<br>");
document.write("x+y=" + (x + y) + "<br>");
document.write("x-y=" + (x - y) + "<br>");
document.write("x*y=" + (x * y) + "<br>");
document.write("x/y=" + (x / y) + "<br>");
document.write("x%y=" + (x % y) + "<br>");
document.write("x++=" + (x++) + "<br>");
document.write("+x=" + (+x) + "<br>");
document.write("y++=" + (y++) + "<br>");
document.write("+y=" + (+y) + "<br>");
</script>
</head>
<body>
</body>
</html>
第二例
赋值运算符
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js赋值运算符</title>
<script type="text/javascript">
var a=10,b=5;
document.write("a="+a+","+"b="+b+"<br>");
var a=10,b=5;
document.write("a="+a+"<br>");
var a=10,b=5;
document.write("a+=b="+(a+=b)+"<br>");
var a=10,b=5;
document.write("a-=b="+(a-=b)+"<br>");
var a=10,b=5;
document.write("a*=b="+(a*=b)+"<br>");
var a=10,b=5;
document.write("a/=b="+(a/=b)+"<br>");
var a=10,b=5;
document.write("a%=b="+(a%=b)+"<br>");
</script>
</head>
<body>
</body>
</html>
第三例
比较运算符
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js比较运算符</title>
<script type="text/javascript">
var a=10,b=5;
document.write("a="+a+","+"b="+b+"<br>");
var a=10,b=5;
document.write("a==10,结果"+(a==10)+"<br>");
var a=10,b=5;
document.write("a=='10',结果"+(a=='10')+"<br>");
var a=10,b=5;
document.write("a===10,结果"+(a===10)+"<br>");
var a=10,b=5;
document.write("a!=10,结果"+(a!=10)+"<br>");
var a=10,b=5;
document.write("a!='10',结果"+(a!='10')+"<br>");
var a=10,b=5;
document.write("a!==10,结果"+(a!==10)+"<br>");
var a=10,b=5;
document.write("a!=='10',结果"+(a!=='10')+"<br>");
var a=10,b=5;
document.write("a>b,结果"+(a>b)+"<br>");
var a=10,b=5;
document.write("a<b,结果"+(a<b)+"<br>");
var a=10,b=5;
document.write("a>=b,结果"+(a>=b)+"<br>");
var a=10,b=5;
document.write("a>=b,结果"+(a<=b)+"<br>");
</script>
</head>
<body>
</body>
</html>
第四例
逻辑运算符
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js逻辑运算符</title>
<script type="text/javascript">
var a=10,b=5;
document.write("a="+a+","+"b="+b+"<br>");
//与 有一个为假都为假
var a=10,b=5;
document.write("a&it<11&&y>6,结果"+(a<11&&b>6)+"<br>");
//或 有一个为真 都为真
var a=10,b=5;
document.write("a&it<11||y>6,结果"+(a<11||b>6)+"<br>");
//取反
var a=10,b=5;
document.write("!(a&it<11),结果"+!(a<11)+"<br>");
</script>
</head>
<body>
</body>
</html>
第五例
三目运算符
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js三目运算符</title>
<script type="text/javascript">
var a=10,b=5;
document.write("a="+a+","+"b="+b+"<br>");
var a=10,b=5;
document.write("a>>11?a:b,结果"+(a>11?a:b)+"<br>");
</script>
</head>
<body>
</body>
</html>
第六例
字符串连接
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js字符串连接</title>
<script type="text/javascript">
var a="你好,",b="世界!";
document.write("a="+a+","+"b="+b+"<br>");
document.write("a+b=,结果:"+(a+b)+"<br>");
</script>
</head>
<body>
</body>
</html>
第七例
if..else语句
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>if...eles语句</title>
<script type="text/javascript">
var x=30;
if(x<25){
alert("x<25")
}else{
alert("x>25")
}
var x=6;
if(x>=1&&x<=3){
alert("春季");
}else if(x>=4&&x<=6){
alert("夏季");
}else if(x>=7&&x<=9){
alert("秋季");
}else if(x>=10&&x<=12){
alert("冬季");
}else{
alert("输入有误");
}
</script>
</head>
<body>
</body>
</html>
第八例
switch语句
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>switch语句</title>
<script type="text/javascript">
var x = 4;
switch (x) {
case 1:alert("周一");break;
case 2:alert("周二");break;
case 3:alert("周三");break;
case 4:alert("周四");break;
case 5:alert("周五");break;
case 6:alert("周六");break;
case 7:alert("周末");break;
default:alert("输入有误!");
}
</script>
</head>
<body>
</body>
</html>
第九例
for循环语句
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>for循环语句</title>
<script type="text/javascript">
for (var x = 1; x <= 20; x += 1) {
document.write("x=" + x + "<br>")
}
</script>
</head>
<body>
</body>
</html>
第十例
while语句 do..while语句
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>while语句 do..while语句</title>
<script type="text/javascript">
var x=1;
while(x<10){
document.write("x="+x+"<br>")
x+=2;
}
var y=1;
do{
document.write("y="+y+"<br>")
y++;
}while(y<10)
</script>
</head>
<body>
</body>
</html>
第十一例
js函数方法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js函数</title>
<script type="text/javascript">
function fun1() {
alert("第一种方法");
}
function fun2(show) {
alert(show);
}
function fun3(a,b) {
alert(a+""+b);
}
function fun4(a,b) {
return a+""+b;
}
//fun1();
//fun2("你好!");
//fun3("你好","世界!");
var s=fun4("1"+"2!");
alert(s);
</script>
</head>
<body>
</body>
</html>