你想知道的类型转换这里都有!
var bar=true;
console.log(bar+0);//1
console.log(bar+"xyz");//truexyz
console.log(bar+true);//2
console.log(bar+false);//1
console.log('1'>bar);//false
console.log(1+'2'+false);//12false
console.log('2' + ['koala',1]);//2koala,1
var obj1 = {
a:1,
b:2
}
console.log('2'+obj1);//2[object Object]
var obj2 = {
toString:function(){
return 'a'
}
}
console.log('2'+obj2)//2a
var b=1;
function outer(){
var b=2;
function inner(){
b++;
console.log(b);//NAN 变量提升,b=undefined
var b=3;
}
inner();
}
outer();//NAN
var b=1;
function outer(){
var b=2;
function inner(){
b++;
console.log(b);
}
inner();
}
outer();//3
var str = new String('123');
console.log(str.valueOf());//123
var num = new Number(123);
console.log(num.valueOf());//123
var date = new Date();
console.log(date.valueOf()); //1526990889729
var bool = new Boolean('123');
console.log(bool.valueOf());//true
var obj = new Object({valueOf:()=>{
return 1
}})
console.log(obj.valueOf());//1
String(null) // 'null'
String(undefined) // 'undefined'
String(true) // 'true'
String(1) // '1'
String(-1) // '-1'
String(0) // '0'
String(-0) // '0'
String(Math.pow(1000,10)) // '1e+30'
String(Infinity) // 'Infinity'
String(-Infinity) // '-Infinity'
String({}) // '[object Object]'
String([1,[2,3]]) // '1,2,3'
String(['koala',1]) //koala,1
Boolean(undefined) // false
Boolean(null) // false
Boolean(0) // false
Boolean(+0) // false
Boolean(-0) // false
Boolean(NaN) // false
Boolean('') // false
Boolean({}) // true
Boolean([]) // true
Boolean(new Boolean(false)) // true
'2' + 1 // '21'
'2' + true // "2true"
'2' + false // "2false"
'2' + undefined // "2undefined"
'2' + null // "2null"
var obj2 = {
toString:function(){
return 'a'
}
}
console.log('2'+obj2)//输出结果2a
var obj1 = {
a:1,
b:2
}
console.log('2'+obj1);//输出结果 2[object Object]
'2' + {} // "2[object Object]"
'2' + [] // "2"
'2' + function (){} // "2function (){}"
'2' + ['koala',1] // 2koala,1
true + 0 // 1
true + true // 2
true + false //1
'5' - '2' // 3
'5' * '2' // 10
true - 1 // 0
false - 1 // -1
'1' - 1 // 0
'5' * [] // 0
false / '5' // 0
'abc' - 1 // NaN
null + 1 // 1
undefined + 1 // NaN
//一元运算符(注意点)
+'abc' // NaN
-'abc' // NaN
+true // 1
-false // 0
var obj1 = {
valueOf:function(){
return '1'
}
}
1 == obj1 //true
3 == 1 false
0 == 0 true
3 == true // false
'0' == false //true
0 == 0 true
'0' == 0 //true
typeof 'seymoe' // 'string'
typeof true // 'boolean'
typeof 10 // 'number'
typeof Symbol() // 'symbol'
typeof null // 'object' 无法判定是否为 null
typeof undefined // 'undefined'
typeof {} // 'object'
typeof [] // 'object'
typeof(() => {}) // 'function'
[] instanceof Array // true
({}) instanceof Object // true
(()=>{}) instanceof Function // true
Object.prototype.toString.call({}) // '[object Object]'
Object.prototype.toString.call([]) // '[object Array]'
Object.prototype.toString.call(() => {}) // '[object Function]'
Object.prototype.toString.call('seymoe') // '[object String]'
Object.prototype.toString.call(1) // '[object Number]'
Object.prototype.toString.call(true) // '[object Boolean]'
Object.prototype.toString.call(Symbol()) // '[object Symbol]'
Object.prototype.toString.call(null) // '[object Null]'
Object.prototype.toString.call(undefined) // '[object Undefined]'
Object.prototype.toString.call(new Date()) // '[object Date]'
Object.prototype.toString.call(Math) // '[object Math]'
Object.prototype.toString.call(new Set()) // '[object Set]'
Object.prototype.toString.call(new WeakSet()) // '[object WeakSet]'
Object.prototype.toString.call(new Map()) // '[object Map]'
Object.prototype.toString.call(new WeakMap()) // '[object WeakMap]'
//NAN的情况
Infinity / Infinity; // 无穷大除以无穷大
Math.sqrt(-1); // 给任意负数做开方运算
'a' - 1; // 算数运算符与不是数字或无法转换为数字的操作数一起使用
'a' * 1;
'a' / 1;
parseInt('a'); // 字符串解析成数字
parseFloat('a');
Number('a'); //NaN
'abc' - 1 // NaN
undefined + 1 // NaN
//一元运算符(注意点)
+'abc' // NaN
-'abc' // NaN
console.log(null.toString())//报错 TypeError: Cannot read property 'toString' of null
console.log(undefined.toString())//报错 TypeError: Cannot read property 'toString' of undefined
console.log(String(null));// null
console.log(String(undefined));// undefined