typeof 用以获取一个变量或者表达式的类型 typeof 一般只能返回如下几个结果 number string boolean object undefined function 注:null和数组为object类型
var str = "123"; console.log(typeof str);// string var strB = "true"; console.log(typeof strB);// string var number = 123; console.log(typeof number);// number var boo = true; console.log(typeof boo);// boolean var oArr = []; console.log(typeof oArr);// object var oNull = null; console.log(typeof(oNull));// object var und = undefined; console.log(typeof und);//undefined var fun = function() {}; console.log(typeof fun);// functiontypeof () typeof
// // typeof number string boolean object undefined function // // typeof(oNull) typeof oNull var str = "123"; console.log(typeof str);// string var strB = "true"; console.log(typeof strB);// string var number = 123; console.log(typeof number);// number var boo = true; console.log(typeof boo);// boolean var oArr = []; console.log(typeof oArr);// object var oNull = null; console.log(typeof(oNull));// object var und = undefined; console.log(typeof und);//undefined var fun = function() {}; console.log(typeof fun);// functionNumber
// 类型转换 // Number var num = Number("123"); console.log(typeof num + ": " + num); var numT = Number(true); console.log(typeof numT);// 1 var numF = Number(false); console.log(typeof numF);// 0 var numN = Number(null); console.log(typeof numN);// 0 var numUnd = Number(undefined); console.log(typeof numUnd);// NaN var numA = Number("a"); console.log(typeof numA);// NaN var numFu = Number("-123"); console.log(typeof numFu);// -123parseInt
将内容转换成整型以目标进制为基底转换成十进制 10当成16进制转换成十进制的数有一个特点,从数字位开始,一直到非数字位截止,将之前的数据返回 // // parseInt 将内容转换成整型 var parsenum = parseInt("123"); console.log(typeof parsenum + ": " + parsenum); var parseNumD = parseInt(123.9); console.log(parseNumD);// 123 // 16 为进制 // 以目标进制为基底转换成十进制 10当成16进制转换成十进制的数 var parseI = parseInt("10", 16); var parseB = parseInt("b", 16); console.log(parseB)// 11 // parseInt 有一个特点,从数字位开始,一直到非数字位截止,将之前的数据返回 var parseNumS = parseInt("123abc"); console.log(parseNumS);// 123 var parsenumT = parseInt(true); console.log(typeof parsenumT);// NaN var parsenumF = parseInt(false); console.log(typeof parsenumF);// NaN var parsenumN = parseInt(null); console.log(typeof parsenumN);// NaN var parsenumUnd = parseInt(undefined); console.log(typeof parsenumUnd);// NaN var parsenumA = parseInt("a"); console.log(typeof parsenumA);// NaN var parsenumFu = parseInt("-123"); console.log(typeof parsenumFu);// NaNparseFloat
// parseFloat var num = parseFloat("123.12ab"); console.log(typeof num + ": " + num);// 123.12 var num = parseFloat("123.ab"); console.log(typeof num + ": " + num);// 123String
// string var str = String(123); console.log(typeof str);// stringBoolean
// boolean var boo = Boolean(1); console.log(typeof boo);// truetoString
toString undefined 和 null不能用toString把20以10进制为基底转换成八进制 // toString undefined 和 null不能用toString var a = 123; var str = a.toString(); console.log(typeof str);// string // 把20以10进制为基底转换成八进制 var a = 20; var str = a.toString(8); console.log(typeof str);// 24// 进制将一个二进制的数 转换成 十六进制的数 // 进制转换
var num = 1000; // 将数值由目标进制转换成十进制 var ten = parseInt(num, 2); // 将十进制转换成目标进制 ten.toString(16); 隐式类型转换 isNaN() 将数值拿出来放到 Number()中,将得到的结果和NaN比对,如果是NaN的话则为true++/-- +/- (一元正负)先调用Number() 在进行操作 注:最后的类型肯定会变成number 当加号两边有一个为string的话,他就会调用String() -*/%&& || !< > <= >= 如果有数字,则会将两边的都转换成数字,如果两边都诶呦数字,则按着ascii码进行比较== != // isNaN --将数值拿出来放到 Number()中,将得到的结果和NaN比对,如果是NaN的话则为true console.log(isNaN(NaN));//true console.log(isNaN("123"));//false console.log(isNaN(123));//false console.log(isNaN("abc"));//true console.log(isNaN("null"));//false // ++ -- + - // ++ -- 先调用Number() 在进行操作 var a = +"abc"; console.log(a + ": " + typeof(a) );// number // + 当加号两边有一个为string的话,他就会调用String() var b = 1 +"abc"; console.log(b + ": " + typeof(b) );// string // -*/% 先调用Number() var c = "1" * 1; console.log(c + ": " + typeof(c) );// number var d = "a" * 1; console.log(d + ": " + typeof(d) );// NaN //&& || ! // < > <= >= 如果有数字,则会将两边的都转换成数字,如果两边都诶呦数字,则按着ascii码进行比较 // 1 == true 成立 // 好玩的 系统定义的 // undefined > 0 false // undefined < 0 false // undefined == 0 false // null > 0 false // null < 0 false // null == 0 false // null = undefined true // NaN = NaN false绝对等于,不进行类型隐式转换的 ===
// 1 !== "1" true // 1 !== 1 false // 1 === 1 true // 1 === "1" false // 特殊的 // NaN === NaN false注: 将未定义的值,去使用,会报错 变量名 is not defined 但是如果将未定义的值放到typeof中,则返回undefined // console.log(typeof(yz)); // “undefined” // console.log(typeof(typeof(yz)));// string
toFixed(re);保留几位小数,并四舍五入
var num = 123.12566 document.write(num.toFixed(3));/123.126
var str = false + 1; console.log(str); //1 var demo = false == 1; console.log(demo); // false if(typeof(a) && -true + (+undefined) + ""){ // "undefined" && "NaN" console.log('基础扎实'); // 打印 } if(11 + "11" * 2 == 33){ //11 + 22 console.log("基础扎实"); // 打印 } // " " true "" false // true + false - false !!" " + !!"" - !!false || console.log("打印????"); // 不打印