简单数据类型:Undefined,Null,Boolean,Number,String
复杂数据类型 Object
typeof是一个操作符而不是函数
eg:var message="some string";
console.log(typeof message);
console.log(typeof (message));
console.log(typeof 95);
var messages;
console.log(messages==undefined);
console.log(typeof age);
Null类型
eg: var car =null;
console.log(typeof car);
undefined派生自null值,因此对他们的相等性测试要返回true;
console.log(null==undefined); //true
Boolean 类型 true不一定等于1,false不一定等于0,转型函数Boolean()
var message="Hello world!";
var messageAsBoolean=Boolean(message);
console.log(messageAsBoolean);
Number类型
var a=0.2;
var b=0.1;
console.log(a+b==0.3);
NaN 非数值是一个特殊的数值,用于表示一个本来要返回数值的操作数未返回数值的情况(这样就不会抛出错误了);
console.log("NaN==NaN:"+(NaN==NaN));
console.log(isNaN(NaN));//true
console.log(isNaN(10));//false
console.log(isNaN("blue"));//true
数值转换,有三个函数可以把非数值转换为数值:Number(),parseInt(),parseFloat();
//Number()能转换字符串和非字符串的数据为数字
//剩下两个函数只能将字符串转为shuzi,parseInt只识别数字类型字符
var num1=Number("Hello world");//NaN
var num1=Number("");//0
var num1=Number("0011");//11
var num1=Number(true);//1
var num1=parseInt("1234blue"); //1234
var num1=parseInt("");//NaN
var num1=parseInt("0xA");//10
var num1=parseInt(22.5);//22
var num1=parseInt("070");//56
var num1=parseInt("70");//70
var num1=parseInt("0xf");//15
parseFloat()方法
console.log(parseFloat("12345blue"));
console.log(parseFloat("0xA"));
console.log(parseFloat("22.5"));
console.log(parseFloat("22.3.4"));
console.log(parseFloat("0908.5"));
console.log(parseFloat("3.125e7"));
String类型,toString()方法转换为字符串
Object类型,就是一组数据和功能的集合
布尔操作符
console.log(!false);
console.log(!"blue");
console.log(!0);
console.log(!NaN);
console.log(!"");
console.log(!12345);
if语句
if (condition) st1 else st2
do-while语句 do{ statement} while(expression)
while语句 while(expression) statement
for语句 for(initialization;expression;post-loop-expression) statement
for-in语句
switch语句
var num = 25;
switch (true) {
case num < 0:
console.log("less than 0.");
case num >= 0 && num <= 10:
console.log("betweent 0 and 10.");
case num > 10 && num <= 20:
console.log("Between 10 and 20.");
default:
console.log("more than 20.");
}
函数 arguments参数的判断
function howManyArgs(){
console.log(arguments.length);
}
howManyArgs("string",45); //2
howManyArgs(); //0
howManyArgs(12);//1