一、typeof 方法
console.log(typeof (123)); //number
console.log(typeof (typeof (123))); //string
console.log(typeof ("123")); //string
console.log(typeof (true)); //boolean
console.log(typeof ({})); //object
console.log(typeof ([])); //object
console.log(typeof (null)); //object null 是一个bug,是空对象的指针和占位符
console.log(typeof (undefined)); //undefined
console.log(typeof (function () { })); //function
// console.log(a); //a is not defined
console.log(typeof (a)); //undefined
console.log(typeof (typeof (a))); //string
console.log(typeof (undefined)); //undefined
console.log(typeof (1 - "1")); //number
console.log(typeof ("1" - "1")); //number
console.log(typeof (2 - "1")); //number
二、显示类型转换
// 转换成数值类型
console.log(Number('123')); //123
console.log(Number('123') + '-' + typeof (Number('123'))); //123-number
console.log(Number("true") + '-' + typeof (Number("true"))); //NaN-number
console.log(Number(true) + '-' + typeof (Number(true))); //1-number
console.log(Number(null) + '-' + typeof (Number(null))); //0-number
console.log(Number(undefined) + '-' + typeof (Number(undefined))); //NaN-number
console.log(Number('a') + '-' + typeof (Number('a'))); //NaN-number
// 转换成整型
console.log(parseInt('12312') + '-' + typeof (parseInt('123'))); //12312-number
console.log(parseInt('true') + '-' + typeof (parseInt('true'))); //NaN-number
console.log(parseInt(true) + '-' + typeof (parseInt(true))); //NaN-number
console.log(parseInt(null) + '-' + typeof (parseInt(null))); //NaN-number
console.log(parseInt(undefined) + '-' + typeof (parseInt(undefined))); //NaN-number
console.log(parseInt('3.14') + '-' + typeof (parseInt('3.14'))); //NaN-number
// 以十六进制为基础转换成十进制
console.log(parseInt(11, 16)); //17
console.log(parseInt('b', 16)); //11
console.log(parseInt('123abc')); //123
console.log(parseInt('abc123')); //NaN
console.log(parseInt('5bc123')); //5
console.log(parseFloat('3.1415956')); //3.1415956
console.log(parseFloat('3.1465956').toFixed(2)); //3.15 四舍五入
console.log(typeof (String(123))); //string
// undefined null 没有 toString() 这个方法
console.log(parseInt('100', 2)); //4
console.log(parseInt('100', 2).toString(16)); //4 以 2进制为基础转换成10进制,再转换成16进制
console.log(Boolean(1)); //true
三、隐示类型转换
var a = '123'
a++
console.log(a); //124
var a = '1' + 1
console.log(a); //11
var a = '3' * 2
console.log(a); //6 * / - % 都是str 转 number
var a = '1' > 2
console.log(a); //false
var a = 1 > '2'
console.log(a); //false
var a = 'c' > 'd'
console.log(a); //false
var a = 'c' < 'd'
console.log(a); //true
var a = 1 != '2'
console.log(a); //true
var c = 1 === '1'
console.log(c); //false
var d = NaN === NaN
console.log(d);
var a = 1 != '2'
console.log(a); //true
var c = 1 === '1'
console.log(c); //false
var d = NaN == NaN
console.log(d); //false NaN不等于任何
var a1 = undefined == 0
var a2 = null == 0
var a3 = null == undefined
console.log(a1, a2, a3); //false false true
console.log(isNaN('a')); //true
console.log(isNaN(null)); //false
console.log(isNaN(NaN)); //true
console.log(isNaN(undefined)); //true
四、
ASCII码 表一 0-127 表二 128-255 都是一个字节
UNICODE码 涵盖 ASCII码 256 开始是两个字节
var str = 'A'
var pos = str.charCodeAt(0)
console.log(pos); //65
var str1 = 'abc'
var pos1 = str1.charCodeAt(2)
console.log(pos1); //99
//求字符串的字节
var getBytes = function (str) {
var bytes = 0
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) <= 255) {
bytes++
} else {
bytes += 2
}
}
return bytes
}
console.log(getBytes('124hello你好!')); //14
var getBytes = function (str) {
// 获取字符串的长度
var strBytes = str.length
for (var i = 0; i < str.length; i++) {
var pos = str.charCodeAt(i)
// 英文字符是一个字节,中文字符是两个字节
// 如果是有一个汉字,那长度就要加一
if (pos > 255) {
strBytes++
}
}
return strBytes
}
console.log(getBytes('124hello你好!')); //14
网友评论