数据类型值得就是字面量的类型
在JS中一共有六种数据类型
String
字符串,表示信息流Number
数值,Boolean
布尔值,表示Null
空值Undefined
未定义,Object
对象,表示符合结构的数据值
其中String 字符串
、Number 数值
、Boolean 布尔值
、Null 空值
、Undefined 未定义
,属于基本数据类型,而Object 对象
,属于引用数据类型
String
字符串
在js
中字符串需要使用引号引起来
使用双引号或单引号都可以,但是不要混用
引号不能嵌套,双引号不能放双引号,单引号不能放单引号
在字符串中我们可以使用\作为 转义字符,当表示一些特殊的符号时可以使用\进行转义
\“
表示”
\‘
表示’
\n
表示换行
\t
表示制表符
\\
表示斜杆\
str="我说\“今天天气真不错!\";
//输出字面量 字符串str
alter("str");
//输出变量str
alter(str);
Number
数值
在JS
中所有的数值都是Number
类型,包括整数和浮点数(小数)
var a=123;//数字123
var b="123";//字符串123
可以使用一个运算符typeof
,来检查一个变量的类型
语法:
typeof 变量
console.log(typeof b);
检查字符串时,会返回string
检查数字时,会返回number
JS
中可以表示数字的最大值:Number.Max_VALUE
最小值:Number.Min_VALUE
大于0的最小值
如果Number
表示的数,超过了最大值,则会返回一个
Infinity
表示正无穷,
-Infinity
表示负无穷,
使用typeof检查元素的时候还是会返回number
NAN
非数字(是一个特殊的数字,表示not a number
),使用typeof检查元素的时候也会返回number
var a=1234+4569;
console.log(a);
在JS中整数的运算基本可以保证精确
var b=0.1+0.3;
console.log(b);
如果使用JS进行浮点元素,可能得到一个不精确的值(结果):(所有的语言差不多都有这个问题),所以千万不要使用JS进行对精确度要求比较高的运算
Boolean
布尔值
布尔值只有两个:(主要用来表示逻辑判断)
-true
:表示逻辑上的真
-false
:表示逻辑上的假
var bool=true;//boolean
//var bool=false;//false
//var bool="false";//string
console.log(typeof bool);
console.log(bool);
使用typeof
检查一个布尔值,会返回boolean
Null
空值
null
类型的值只有一个,就是null
null
这个值专门用来表示一个空的对象
使用typeof
检查一个null时,会返回object
var a=null;
console.log(a);//
Undefined
未定义
只有一个值:就是undefined
当声明一个变量,但是并不给它赋值时,它的值就是undefined
使用typeof
检查一个undefined时,也会返回undefined
var b;
//var b=undefined;
console.log( b)
//console.log(typeof b)
强制类型转换
–指将一个数据类型强制类型转换为其他的数据类型
–类型转换主要指,将其他的数据类型,转换为:
String Number Boolean
将其他的数据类型转换成String
var a=123;
console.log(typeof a);
console.log(a);
- –该方法不会影响到 原变量,它会将转换的结果返回
- –这个null和undefined两个值,没有toString()方法
//调用a的toString()方法
//调用xxx.yyy()方法,就是xxx.yyy()
var a=123;
var b=a.toString();
console.log(typeof b);
console.log(b);
2.调用String()函数
–使用这个做强制类型转换的时候,
对于Number和Boolean实际上就是调用的toString()方法,但是对于null和undefined,就不会调用toSring()方法,他会将null(字面量)直接转换为“null”(字符串)
将undefined直接转换为“undefined”
a=null;
a=String(a);
console.log(typeof a);
console.log(a);
a=undefined;
a=String(a);
console.log(typeof a);
console.log(a);
将其他类型转换为Number
1.使用Number()函数(使用和String()
函数一样)
- 字符串—>数字
1》如果是纯数字的字符串,则直接将其转化为数
2》如果字符串中有非数字的内容,则转换为NaN
3》如果字符串是一个空串或者是一个全是空格的字符,则转换为0
var a=123;
a=Number(a);
console.log(typeof a);
console.log(a);
- 布尔值—>数字
- true转换为1
- false转换为0
a=true;
a=Number(a);
console.log(typeof a);
console.log(a);
b=false;
b=Number(b);
console.log(typeof b);
console.log(b);
- Null—>数字
- 转换为0
a=null;
a=Number(a);
console.log(typeof a);
console.log(a);
- undefined—>数字
- NaN
a=undefined;
a=Number(a);
console.log(typeof a);
console.log(a);
2.这种方式专门用来对付字符串
- parseInt()可以将一个字符串中的有效的整数内容取出来,然后转换为Number
//调用parseInt()函数转换为Number
a="123px";
a=parseInt(a);
console.log(typeof a);
console.log(a);
- parseFloat()函数作用和parseInt()类似,不同的是它可以获得有效的小数
//调用parseFloat()函数转换为Number
a="123.465px";
a=parseInt(a);
console.log(typeof a);
console.log(a);
- 如果在对非
String
使用parseInt()
或者parseFloat()
,它会先将其转换为String
,然后在进行操作
a=true;
a=parseInt(a);
console.log(typeof a);
console.log(a);
将其他的数据转换为Boolean
使用Boolean()函数
- 数字—>布尔
- 除了0和
NaN
都是true
- 除了0和
a=123;//true
a=-123;//true
a=0;//false
a=Infinity;//true
a=Boolean(a);
console.log(typeof a);
console.log(a);
string
—>布尔- 除了空串都是
true
- 除了空串都是
a="";//false
a="123";//t
a="true";//t
a=Boolean(a);
console.log(typeof a);
console.log(a);
-null
和undefined
—>布尔
- 都是
false
a=null;//f
a=undefined;//f
a=Boolean(a);
console.log(typeof a);
console.log(a);
- 对象也会转换为
true