数据类型
数据类型
JavaScript中分为:原始类型和引用类型
5种原始类型:
number: 数字(整数、小数、NaN(Not a Number))string: 字符、字符串,单双引号皆可boolean: 布尔。true, falsenull: 对象为空undefined: 当声明的变量未初始化时,该变量的默认值是undefined
使用typeof运算符可以获取数据类型
类型转换
- 其他类型转为
number:string: 按照字符串的字面值,转为数字,如果字面值不是数字,则转为NaN。一般使用parseInt。如alert(parseInt(str) + 1)- boolean: true转为1,false转为0
- 其他类型转为boolean:
- number: 0和NaN转为false,其他的数字转为true
- string: 空字符串转为false,其他的字符串转为true
- null:false
- undefied: 转为false