JavaScript_数据类型转换
问题
- JavaScript是数据类型有哪些?
- parseInt() parseFloat() Number()转换的区别是什么?
- != !==的区别是什么?
parseInt() parseFloat() Number()
const a = 12.35;
console.log(parseInt(a)); //12
const a1 = '12.7as34';
console.log(parseInt(a1)); //12
console.log(parseFloat(a1)); //12.7
const b = null;
console.log(Number(b)); //0
const c = undefined;
console.log(Number(c)); //NaN
String() Boolean()
console.log(typeof String(123)); //string
console.log(String(3.1415926e2)); //314.15926
console.log(String(null)); //null
console.log(String(undefined)); //undefined
console.log(String.fromCharCode(97)); //a
console.log(Boolean(0.123)); //true
console.log(Boolean(0)); //false
console.log(Boolean('')); //false
console.log(Boolean(' ')); //true
console.log(Boolean(null)); //false
console.log(Boolean(undefined)); //false
自动转换,隐式转换
console.log(" "+ 123); //"123"
console.log(" 123 " - 1); // 122 +-*/%,++,--
console.log(true+1); //2
console.log(" 12 3 " - 1); // NaN
%,++
console.log(12.1 % 2); //0.09999999999999964
console.log(-12 % 5); // -2
console.log(12 % (-5)); // 2
let a = 10;
let b;
b = a++;
// b = ++a;
console.log(a); //11
console.log(b); //10
关系运算符 == === > >= < <= != !== 赋值运算符 逻辑运算符 && || & | typeof ?:
console.log('12' != 12); //false
console.log('12' !== 12); //true
//如果表达式1为真,那么就直接返回表达式2的值
console.log(10 && 0); //0
console.log(0 && 12); //0
console.log(10 || 0); //10
console.log(!10 || 0); //0
console.log(0 || 12); //12
console.log(!0 || 12); //true
let i = 12;
console.log(12 < 11 && i++); //false
console.log(i); //12
let i = 12;
console.log(12 < 11 & i++); //0
console.log(i); //13
let i = 12;
console.log(12 < 11 | i++); //12
console.log(i); //13
let i = 12;
console.log(12 < 11 | ++i); //13
console.log(i); //13
let i = 12;
console.log((i++,++i,i++)); //14
null与undefined
console.log(null == "");
console.log(null == false);
console.log(null == undefined); //true
console.log(null === undefined);
console.log(null == 0);
//if(a != null) null与undefined过滤掉了