目录
一、数据类型
在js中一共有六种数据类型:String 字符串;Number 数值;Boolean布尔值;Null 空值;Undefined未定义;Object对象。
基本数据类型 | String | Number | Boolean | Null | Undefined |
引用数据类型 | Object |
1.String类型:
(1):字符串的 length 只可以获取,不可以设置。
(2)在JS中字符串需要使用引号引起来,使用双引号或单引号都可以,但是不能混着用
var str = 'hello';
var str1 = "hei";
(3)引号不能嵌套,双引号不能放双引号,单引号不能放单引号
var str2 = "我说:'今天很开心'";
转义字符:在字符串中我们可以使用\作为转义字符,当表示一些特殊符号时可以使用\进行转义
转义字符表
字符 | 含义 |
\" | " |
\' | ' |
\n | 换行符 |
\t | 空格符 |
\\ | \ |
- \"
var str3 = "我说:\"今天天气好\"";
console.log(str3);
// 结果:我说:"今天天气好"
- \'
var str3 = "我说:\'今天天气好\'";
console.log(str3);
// 结果:我说:'今天天气好'
- \n
var str3 = "我说:\n今天天气好";
console.log(str3);
// 结果:
// 我说:
// 今天天气好
- \t
var str4 = "今天天\t气不错"
console.log(str4);
// 结果:今天天 气不错
- \\
var strr = "\\";
console.log(strr);
// 结果:\
2.Number类型
(1)在js中所有的数值都是Number类型,包括整数和浮点数。
(2) js中可以表示的数字的最大值:Number.MAX_VALUE = 1.7976931348623157e+308。
如果使用Number表示的数字超过了最大值,则会返回一个Infinity 表示正无穷,Infinity是字面量,-Infinity 表示负无穷;可以表示的数字0以上的最小值:Number.MIN_VALUE = 5e-324。
(3)可以运用运算符typeof来检测一个变量的类型:typeof 变量。检查字符串会返回String,检查数字会返回Number。
var b = 123;
console.log(typeof b); // number
使用typeof检查Infinity会返回Number。
console.log(typeof Infinity); // number
(4)NaN是一个特殊的数字,表示not a number,使用typeof检查NaN也会返回number
var num = "ab" * "bc";
console.log(num); // NaN
console.log(typeof num); // number
(5)在JS中对数字进行运算
对整数进行运算
var c = 12 + 543;
console.log(c); // 555
对小数进行运算
var d = 0.1 + 0.2;
console.log(d); // 0.30000000000000004
注意:在jS中整数的运算基本可以保证精确。但是使用JS进行浮点运算,可能得到一个不精确的结果,所以千万不要使用JS进行对精确度要求比较高的运算。
3.Boolean类型
boolean类型有两个值:true和false。
使用typeof检查一个布尔值时,会返回boolean
var bool = true;
console.log(typeof bool); // boolean
注:如果给值加上引号,就是string类型
var bool1 = "true";
console.log(typeof bool1); // string
4.Null类型
null类型专门用来表示一个空的对象。
var a = null;
console.log(a); // null
使用typeof检查一个null值时,会返回object类型
console.log(typeof a); // object
5.Undefined类型
当声明一个变量,但是并不给变量赋值时,它的值就是undefined
var b;
console.log(b); // undefined
使用typeof检查一个undefined时,会返回undefined
console.log(typeof b); // undefined
undefined和null值相等, 类型不等
console.log(null == undefined); // true
console.log(null === undefined); // false
二、数据类型转换
类型转换主要指将其他的数据类型,转换为string Number Boolean。
数据类型转换
类型 | 方式 | 转换类型 | 例子 | 值 |
转换为String | 调用toString()方法 | Number-->String | 123 | "123" |
调用String()函数 | ||||
转换为Number | 调用Number()函数 | String-->Number | "12" | 12 |
String-->Number | "12a" | NaN | ||
String-->Number | ""(空串) | 0 | ||
String-->Number | " "(空格) | 0 | ||
Boolean-->Number | true | 1 | ||
false | 0 | |||
Null-->Number | Null | 0 | ||
Undefined-->Number | Undefined | NaN | ||
整数调用parseInt()函数 | String-->Number | "01123.3px" | 1123 | |
浮点数调用parseInt()函数 | String-->Number | "1123.4px" | 1123.4 | |
转换为Boolean | 调用Boolean()函数 | Number-->Boolean | 0 | false |
Number-->Boolean | NaN | false | ||
Number-->Boolean | Infinity | true | ||
String-->Boolean | "" | false | ||
String-->Boolean | " " | true | ||
Null-->Boolean | null | false | ||
undefined-->Boolean | undefined | false | ||
Object-->Boolean | --- | true |
1.转换为String类型
(1)调用被转换数据类型的tostring()方法,该方法不会影响原变量,会将转换的结果返回
var a = 123;
console.log(typeof a); // number
a = a.toString();
console.log(typeof a); // string
注意: null和undefined这两个值没有toString()方法
(2)调用String()函数,并将被转换的数据作为参数传递给函数
var b = 34;
console.log(typeof b); // number
b = String(b);
console.log(typeof b); // string
使用String()函数做强制类型转换时,对于Number和Boolean实际上就是调用的toString()方法,但是对于null和undefined,就不会调用toString()方法,它会将null直接转换为"null",将undefined直接转换为"undefined"。
2.转换为Number类型
(1)调用Number()函数
字符串--------》数字
纯数字的字符串类型转数字类型,直接转换为数字
var a = "12";
a = Number(a);
console.log(typeof a); // number
有非数字的字符串类型转数字类型,转换的结果为NaN
a = "12a";
a = Number(a);
console.log(a); // NaN
console.log(typeof a); // number
如果字符串是一个空串或空格,则转换为0
a = "";
a = Number(a);
console.log(a);// 0
console.log(typeof a); // number
a = " ";
a = Number(a)
console.log(a); // 0
console.log(typeof a); // number
布尔---------》数字
true转成1,false转成0
a = true;
a = Number(a);
console.log(a); // 1
console.log(typeof a); // number
a = false;
a = Number(a);
console.log(a); // 0
console.log(typeof a); // 1
null--------》数字
a = null;
a = Number(a);
console.log(a); // 0
console.log(typeof a); // number
undefined---------》数字
a = undefined;
a = Number(a);
console.log(a);// NaN
console.log(typeof a); // number
(2) 调用parseInt()和parsefloat()函数:专门针对字符串
parseInt(): 把一个字符串转换为一个整数
a = "1123.3px";
a = parseInt(a);
console.log(a); // 1123
console.log(typeof a); // number
a = "0001123.3px";
a = parseInt(a);
console.log(a); //1123
parseFloat(): 把一个字符串转换为一个浮点数
a = "1123.4px";
a = parseFloat(a);
console.log(a); // 1123.4
console.log(typeof a); // number
如果对非String使用parseInt()或parseFloat() 它会先将其转换为String然后在操作
a = true;
a = parseInt(a);
console.log(a); // NaN
3.将其他数据类型转换为Boolean
调用Boolean()函数:数字转换为布尔,除了0和NaN,其他都是true
数字-------》布尔:除了0和NaN,其他都是true
var a = 0;
a = Boolean(a);
console.log(a); // false
a = NaN;
a = Boolean(a);
console.log(a); // false
a = Infinity;
a = Boolean(a);
console.log(a); // true
字符串---------》布尔,除了空串,其他都是true,非空为真
a = "";
a = Boolean(a);
console.log(a); // false
a = " ";
a = Boolean(a);
console.log(a); // true
null---------》布尔
a = null;
a = Boolean(a);
console.log(a); // false
undefined---------》布尔
注:对象也会转换为true
3.1转换成布尔类型为false的值有:0、-0、''、undefined、false、NaN、null;
console.log(Boolean(0)); // false
console.log(Boolean(-0)); // false
console.log(Boolean(undefined)); // false
console.log(Boolean(false)); // false
console.log(Boolean(NaN)); // false
console.log(Boolean(null)); // false
console.log(Boolean('')); // false
4.返回undefined的情况:
- 声明的变量没有赋值
- 访问数组不存在的下标
- 访问对象不存在的属性
- 没有给函数的形参传递实参
- 函数没有返回值
- typeof 一个没有声明的变量
三、进制转换
16进制的数字,以0x开头
8进制的数字,以0开头
2进制的数字,以0b开头,但是不是所有的浏览器都支持
例:
var a = "0x123";
a = "0211";
a = "0b342";
将其他进制的数转换为十进制的数,格式:parseInt(a, 8) , 第一个参数是变量,第二个参数是指定当前的数的进制
a = "070"
a = parseInt(a, 8);
console.log(a); // 56