1.数据类型
基本数据类型:共5种,Number,Boolean,String,Null,Undefiend
复杂数据类型:Object
2.typeof操作符:返回值是字符串,并且首字母是小写:"object", "string", "undefined", "number", "boolean","function"
2.1对未初始化或未声明的变量执行typeof操作符会返回字符串undefined
var message;
alert(typeof message); // "undefined"
//age没有声明
alert(typeof age); // "undefined"alert(typeof( typeof age));//"string"但是,
var message; // 这个变量声明之后默认取得了 undefined 值
alert(message); // "undefined"
alert(age); // 产生错误2.2var car = null;
alert(typeof car); // "object"3.类型转化:
3.1显示类型转化:
Number():
如果是布尔值,true和false分别被转换为1和0
如果是数字值,返回本身。
如果是null,返回0.
如果是undefined,返回NaN。
如果是字符串,字符串只包含:小数,十六进制数,或整数(正负都可以),则将其转换为十进制数(忽略前导0);
否则返回NaN。
console.log(Number('123abc'));//NaN
console.log(Number('018'));//18
console.log(Number('0x1A'));//26
console.log(Number('12.3'));//12.3
console.log(Number('.123'));//0.123
console.log(Number('1.2.3'));//NaNparseInt():只有一个参数时只转换开头的数字部分,两个参数时,第二个参数表示进制。
console.log(parseInt(undefined));//NaN
console.log(parseInt(null));//NaN
console.log(parseInt('a'));//NaN
console.log(parseInt('a',16));//10
console.log(parseInt('12',16));//18
console.log(parseInt('a',8));//NaN
console.log(parseInt('12.3a'));//12
console.log(parseInt('12.3',8));//10parseFloat():和parseInt()相似,只不过可以是小数 console.log(parseFloat(null));//NaN
console.log(parseFloat('1.2.3'));//1.2
console.log(parseFloat('-12.3abc'));//-12.3String():将任何类型的值转换为字符串
Boolean():以下值会被转换为false:false、”"、0、NaN、null、undefined,其余任何值都会被转换为true。
toString():除undefined和null之外的所有类型的值都具有toString()方法,其作用是返回对象的字符串表示
var demo=15;
console.log(demo.toString());//"15"
console.log(demo.toString(8));//"17"将二进制转换为十六进制:
var num = 11010;
var test = parseInt(num,2);
var result = test.toString(16);
console.log(typeof(result)+':'+result);//string:1a
isNaN():会自动调用Number()函数,结果与NaN比较
console.log(isNaN(123));//false
console.log(isNaN('123a'));//true
console.log(isNaN('123'));//false
console.log(isNaN(NaN));//true
console.log(isNaN(null));//false
console.log(isNaN('null'));//true
console.log(isNaN(undefined));//true ++ -- -/+(正/负)自动调用Number()+(加):只要有一个是字符串,就调用String()
-*/%(减乘除摩尔):调用Number()
&& || ! :调用Boolean()
== !=有隐式类型转换
< > <= >= 有数字调用Number(),都是字符串进行ASC码比较

570

被折叠的 条评论
为什么被折叠?



