<!DOCTYPE html>
<html lang="en">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 数据类型就是字面量的类型
// 基本数据类型:
// - String 字符串
// - Number 数值
// - Boolean 布尔值
// - Null 空值
// - Undefined 未定义
// 引用数据类型:
// - Object 对象
// 1 String 字符串
//var str = 'good'; 创建一个字符串str,字符串需要要用引号起来,
// str = "how "are" you ";报错,浏览器会识别就近的双引号,以至于are变成未定义的变量了
// str = "how \"are\" you" 可以用\作为转移字符解决上面问题,
//转移字符:
// \\ ---- \
// \n ---- 换行
// \t ---- 制表符,相当于恩了tab键
// \' ---- '
// console.log(str); //也可以console.log('good');
// alert('asdas\nda\tsd');
// console.log('as\ndasd\tasd');
// 2 Number 数值
// 所有整数和浮点数都为数值
var a = 123;
var b = 1.23;
console.log(typeof a); //输出number typeof为运算符 可以检查变量的类型
console.log(typeof b); //输出number
console.log(Number.MAX_VALUE); //输出数值最大值 1.7976931348623157e+308
console.log(Number.MAX_VALUE * Number.MAX_VALUE); //输出为Infinity,正无穷
console.log(-Number.MAX_VALUE * Number.MAX_VALUE); //输出为-Infinity,负无穷
console.log(Number.MIN_VALUE); //输出为5e-324 为大于0的最小的正值
a = Infinity;
console.log(typeof Infinity); //判断Infinity的类型,为数值,控制台输出为number
a = 'asd' * 'asd'; //两个字符串相乘
console.log(a); //返回NaN,not a number非数字
console.log(typeof NaN); //输出为number(返回的number是小写的),所以NaN为数值类型
var c = 111111111111111111111 + 111111111111111;
console.log(c); //输出为111111222222222230000,js中大值的整数运算存在偏差
c = 0.1 + 0.2;
console.log(c); //输出0.30000000000000004 js中进行浮点数运算时,可能得到一个不精准的值,基本所有语言都有
//精度问题,二进制无法精致表示1/10的值,不要用js进行精确度要求较高的运算
// 3 Boolean 布尔值
// true or false 两个值
// 主要做逻辑判断
var bool = true; //不用加引号
console.log(bool); //返回true
console.log(typeof bool); //返回boolean
// 4 Null 空值
//Null类型的值只有一个null
a = null;
console.log(a); //null
console.log(typeof a); //返回为object对象 表示为空的对象
// 5 Undefind 未定义
//输出的结果只有一个undefind
var uname; //声明了变量但未赋值
var uname = undefined;
console.log(uname); //输出undefind 未定义
console.log(typeof uname); //undefind的数据类型为undefind
//Object对象比较复杂后续再学
</script>
</body>
</html>
基本数据类型 String Number Boolean Null Undefind
最新推荐文章于 2022-07-26 20:46:28 发布