JS中的简单数据类型有五种 :
--> string
--> number
-->boolean
--> null
-->undefined
数据类型的检测 :typeof
语法: typeof 数据;
typeof ( 数据 ) ;
1 ?typeof 100; //结果为number
使用typeof获取的数据类型名是字符串类型
1 var num = 100;2 var result = typeof num;//result此时保存的是变量str中数据的类型名3 console.log(typeof result);//"string"
String类型
1 使用成对的"" ‘‘,用于标识的引号是不会显示的,可以使用转义符(\) 显示
2 强制转换
数据.toString();
var num = 100; ???console.log(num.toString()); ???var result = num.toString(); ???console.log(num); ???console.log(result);
问题:
a,遇到数值类型的原值去进行toString等操作时,需要多加一个.符号
var num -100;console.log(100..toString());//num.toString();
b,undefined 和 null 不支持toString的功能
undefined.toString();null.toString();
String(数据);
console.log(String(100));console.log(String(true));console.log(String(null));console.log(String(undefined));
3 隐式转换
使用其他数据类型和字符串类型进行+操作,就可以进行字符链接
console.log("100abc" + 200);//"100abc200"
通常为了不改变数据的原始内容,会使用空字符串进行隐式转换的操作
var num = 100;console.log(num + "");
Number类型
1 整数类型
???2 小数类型
???3 不是数 NaN
???4 强制转换
a, Number(数据);
b, parseInt(数据);
转换为整数
从左边开始,遇到不是数停止,首字符不是数,返回NaN
c, parseFloat(数据);
转换为小数
???5 隐式转换
+ 前面不能有字符
-
*
/
%
Boolean类型
1 true
2 false
只有以下六个值是false,其他的都是true;
0 "" NaN null undefined false
3 强制转换
Boolean(数据);
4 隐式转换
!!数据;
NULL类型
undefined类型
出现的场景
1)变量声明未赋值
??????? 2)数组元素不存在
??????? 3)函数形参没有值
??????? 4)函数的返回值--默认值
??????? 5)对象属性不存在
JaveScript简单数据类型(JS知识点归纳二)
原文地址:http://www.cnblogs.com/AmorR/p/8083039.html