javascript 变量与条件判断

//判断哪些东西被当作false

function fun(a){

    if(!a) alert("not " + a)

    else alert(true)

}

fun();            //not undefined

fun(0);          //not 0

fun("");        //not

fun(false);   //not false

fun(null);    //not null

fun(undefined);   //not undefined(undefined是关键字)

 

//空数组或对象为true

fun([])    //true

fun({})  //true

 

in测试

arr = [11, 22, 33]

obj = ['attr1': 1, 'attr2':2]

if(1 in arr){}           //true, 这里的i是index, 所以这种用法应该避免

if("attr1" in obj){}          or    if(obj['attr1']){}

 

if(a = obj['key']){

//do something with a

}

 

for in 循环

数组循环

arr=[11, 22, 33]

for (var i in arr){

    console.debug(arr[i])     #11, 22, 33. 这里的i 是index

}

 

obj循环

var a={name:'asd',id:123} , b;
for(var obj in a){
    console.debug(obj)     #name, id
}

 

for循环

for (i=0; i<=10; i++){

...........

}

你可能感兴趣的:(JavaScript)