judgement
- if else
switch(){
case 1:
alert();
break;//if no break, goes to case 2;
case 2:
alert();
break;
default:
alert();
}
<exp>?<v1>:<v2>
eg:
max = (a>b)?a:b
loop
while(){
}
var a;
var b;
temp=b;
while(b!=0){
temp=a%b;
a=b;
b=temp;
}
alert("greatest common divisor is"+a);
do{
} while();
//reverse
number= 12334;
reverse=0;
do{
lastDigit=number%10;
reverse=reverse*10+lastDigit;
number=number/10;
}while(number!=0);
alert("the reverse is"+reverse);
for( init; ; ){
}
if for( ; ; ), infinite loop;
break & continue
break: out of the loop;
continue: next of the loop;
function
function name(){
}
function variable
var f = new Function("x","y","return x*y");
**equals to**
function f(x,y){
return x*y}
eg:
function add(a,b){
return a+b;
}
function cal(f,a,b){
return f(a.b);
}
document.write(cal(add,5,6));
- local variable :
inside the function - global variable
unlike C, there’s no other kinds of variables,
i,e, you can’t use nested variables with the same name in a function.