typeof六种数据类型:
number、string、boolean、undefined、object、function
var num = 123;
console.log(typeof(num)); // number
var num1 = 'true';
console.log(typeof(num1)); // string
var num2 = null;
console.log(typeof(num2)); // object
var num3 = true;
console.log(typeof(num3)); // boolean
var num4 = function() {};
console.log(typeof(num4)); // function
var num5 = undefined;
console.log(typeof(num5)); // undefined
显式类型转换
1. Number(mix)
var num = Number('123');
console.log(typeof(num) + ':' + num); // number:123
var demo = undefined;
var num1 = Number(demo);
console.log(typeof(num1) + ":" + num1); // number:NaN
var a = Number('a');
console.log(typeof(a) + ':' + a);// number:NaN
2.parseInt(String,radix)
var demo = 123.123;
var num = parseInt(demo);
console.log(typeof(num) + ':' + num); // number:123
var demo = '123.123';
var num = parseInt(demo);
console.log(typeof(num) + ':' + num); // number:123
var demo = '123.123';
var num = parseInt(demo, 8);
console.log(typeof(num) + ':' + num); // number:83
var demo = 123.123;
var num = parseInt(demo, 2);
console.log(typeof(num) + ':' + num); // number:1
-
parseFloat(String)
var demo = '100.2abcd'; var num = parseFloat(demo); console.log(typeof(num) + ':' + num); // number:100.2
4.toString(radix)
var demo = 1234;
var num = demo.toString();
console.log(typeof(num) + ':' + num); // string:1234
var demo = 20;
var num = demo.toString(8);
console.log(typeof(num) + ':' + num); // string:24
将二进制的 10000 转换成 16 进制
var num = 10000;
var test = parseInt(num, 2);
console.log(test.toString(16)); // 10
undefined, null 不能用toString
String(mix)
var demo = '100.2abcd';
var num = String(demo);
console.log(typeof(num) + ':' + num); // string:100.2abcd
Boolean()
var demo = '100.2abcd';
var num = Boolean(demo);
console.log(typeof(num) + ':' + num); // boolean:true
var demo = '';
var num = Boolean(demo);
console.log(typeof(num) + ':' + num); // boolean:false
隐式类型转换
isNaN() -> Number()
console.log(isNaN(NaN)); // true
console.log(isNaN('abc')); // true
console.log(isNaN(null)); // fslse
console.log(isNaN(undefined)); // true
// Number();
++/-- +/-(一元正负)
var a = '123';
a++;
console.log(a); // 124
var b = 'abc';
b--;
console.log(b); // NaN
var a = +'abc';
console.log(a + ':' + typeof(a)); // NaN:number
-*/% ->number
var a = '1' * 1;
console.log(a + ':' + typeof(a)); // 1:number
var a = 'a' * 1;
//Number(a)*Number(1)
console.log(a + ':' + typeof(a)); // NaN:number
&&||!
<><=>=
var a = '3' > '2';
console.log(a + ':' + typeof(a)); // true:boolean
var a = '3' > 2;
console.log(a + ':' + typeof(a)); // true:boolean
==!=
var a = 1 != true;
console.log(a + ':' + typeof(a)); // false:boolean
undefined == null
// true
NaN == NaN
// false
不发生类型转换
=== !== 绝对等于和绝对不等于
1 === '1'// false
1 === 1 // true
1 !== '1' // true
1 !== 1 // false
NaN === NaN // false
console.log(typeof(a)); // undefined
唯一一个未定义不会报错 将变量放在typeof()里面
作业:
alert(typeof(a)); // undefined
alert(typeof(undefined)); // undefined
alert(typeof(NaN)); // Number
alert(typeof(null)); // Object
alert(typeof(NaN)); // Number
var a = '123abc';
alert(typeof(+a)); // Number
alert(typeof(!!a)); // Boolean
alert(typeof(a + '')); // string
alert(1 == '1'); // true
alert(NaN == NaN); // false
alert(NaN == undefined); // false
alert('11' + 11); // 1111
alert(1 === '1'); // false
alert(parseInt('123abc')); // 123
var num = 123123.3456789;
alert(num.toFixed(3)); // 123123.346