运算符:
1.算术运算符( + - * / %)
加、减、乘、除、求余,其中%是求余运算
var total = (1 + 4) * 5;
var i = 100;
var temp = (i – 20) / 2;
alert(“10”+ 20) //return 1020;
alert(10 + 20) //return 30;
2. 后增量/后减量运算符 ++ ,--
var i = 10;
var a = i++; // i = i + 1;
alert(a);
例:
var i=20;
i+=1;
document.write(i+"<br/>");
var i=20;
i+=10;
document.write(i+"<br/>");
var i=20;
i-=1;
document.write(i+"<br/>");
var i=20;
i-=10;
document.write(i+"<br/>");
3. 比较运算符 ( >, <, >=, <=, ==, !=,===,!== )
alert ( 10 > 5 ); //outputs true
var i = 100;
var n = 100;
alert(i == n); //outputs true;
alert(i != n); //outputs false;
alert(i === n) //数据类型,值相等
例:
var price1=100,
price2=200;
document.write((price1 > price2)+"<br/>");
var price1=200,
price2=100;
document.write((price1 < price2)+"<br/>");
var price1=1199.98999999,
price2=1199.989999990000001;
document.write((price1 < price2)+"<br/>");
var price1=999,
price2=1999;
document.write((price1 <= price2)+"<br/>");
var number1=0,
string1="";
document.write((number1 == string1)+"<br/>");
var number1=0,
number2=false;
document.write((number1 != number2)+"<br/>");
var number1=0,
boolean1=false;
document.write((number1 === boolean1)+"<br/>");
var boolean1=0,
boolean2=false;
document.write((boolean1 !== boolean2)+"<br/>");
4.逻辑运算符( &&, || , !)
&& : 逻辑与
|| : 逻辑或
! :逻辑非
var i = 8;
alert ( i<5 && i<10); //outputs false
alert ( i > 100 || i < 10); //outputs true
alert(!(10 > 5)); //outputs false
例:
var num1=10,
num2=20;
document.write((num1>5 && num2<20)+"<br/>");
var num1=33,
num2=34;
document.write((num1==33 && num2>20)+"<br/>");
var boolean1=false,
boolean2=true;
document.write((boolean1 || !boolean2)+"<br/>");
document.write((((((!boolean1 === boolean2) && !boolean2)|| !boolean1))===boolean2)+"<br/>");
程序流程控制:
一、条件语句 if
语法:
if(condition) statements1 else statement2
例:
if(10 > 5) {
alert(“hello world”);
}
var i=60;
if(i>=80){
alert("成绩优异");
}else if(i<80 && i>=60) {
alert("成绩合格");
}else if(i<60){
alert("成绩不合格");
}
二、switch语句
语法:
switch (expression){
case value:
//statement
break;
case value:
//statement
break;
default:
//statement
}
例:
var i="A";
switch(i){
case 'A':
alert("你已经订购A套餐");
break;
case 'B':
alert("你已经订购B套餐");
break;
case 'C':
alert("你已经订购C套餐");
break;
case 'D':
alert("你已经订购D套餐");
break;
//默认
default:
alert("欢迎下次再来");
}