1. 数值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>数值</title>
</head>
<body>
<script>
/*
数值(Number)
- 在js中,所有的整数和浮点数都是Number类型
- js中的数值并不是无限大的,当超过一定范围后,会显示近似值
- Infinity是一个特殊的数值,表示无穷大
- 进行计算时,需注意精度的问题
*/
let a = 10
a = 100
a = 99999 * 999999
a= Infinity
a = 1 - 'a' //NaN(Not a number)
console.log(a)
/*
大整数(BigInt)
- 用来表示比较大的整数
- 大整数使用n结尾,它可以表示的数字范围是无限大
*/
/*
其他进制的数字
- 二进制 0b
- 八进制 0o
- 十六进制 0x
*/
let b
b = 0b1010
b = 0o10
b = 0xff
console.log(b) //打印时都是十进制
</script>
</body>
</html>
2. 类型检查
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>类型检查</title>
</head>
<body>
<script>
let a = 10
let b = 10n
console.log(a)
console.log(b)
//typeof检查的是变量的值的类型,而不是变量的类型,在js中,变量是没有类型的
//typeof返回的结果是一个字符串
console.log(typeof a)
console.log(typeof b)
</script>
</body>
</html>
3. 字符串
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>字符串</title>
</head>
<body>
<script>
/*
字符串
- 在js中使用单引号或双引号表示字符串
- 转义字符
\ 做斜杠
\t 制表符
\n 换行
- 模板字符串
使用 ` 表示,可以跨行,引号不能跨行
*/
let a = '孙笑川'
a = '这是一个"字符串"'
a = "这是一个\"字符串\""
console.log(a)
let b = `我的名字是:
孙笑川`
console.log(b)
let name = '孙笑川'
let str = `我的名字是${name}`
console.log(str)
</script>
</body>
</html>
4. 其他的数据类型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>其他的数据类型</title>
</head>
<body>
<script>
/*
布尔值(Boolean)
- 用于逻辑判断
- 只有true、false两个值
*/
let flag = false
console.log(flag)
console.log(typeof flag)
/*
空值(Null)
- 表示空对象
- 空值只有一个,即null
- 使用typeof检查空值时,会返回"object",这其实是js发展历史中的一个bug,typeof无法检查
*/
let a = null
console.log(a)
console.log(typeof a)
/*
未定义(Undefined)
- 当声明一个变量没有赋值时,它的值就是undefined
- 使用typeof检查时,返回的是"undefined"
*/
//let b
let b = undefined
console.log(b)
/*
符号(Symbol)
- 用来创建一个唯一的标识符
- 使用typeof检查时,返回的是"Symbol"
*/
let c = Symbol() //调用Symbol函数创建了一个符号
console.log(c)
//这七种数据类型的原始值不可变(即创建之后不可修改)
</script>
</body>
</html>
5. 类型转换 - 字符串
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>类型转换-字符串</title>
</head>
<body>
<script>
/*
将一种数据类型转换为其他类型
*/
let a = 10
a = true
a = 100n
//a = null
console.log(typeof a, a)
/*
1.调用toString()方法,注意:null和undefined没有该方法
*/
a = a.toString()
console.log(typeof a, a)
/*
2.调用String()函数
对于拥有toString()方法的值调用String()函数时,实际上就是在调用toString()
对于null、undefined等值,则是直接进行转换
*/
let b = 10
b = null
b = undefined
console.log(typeof b, b)
b = String(b)
console.log(typeof b, b)
</script>
</body>
</html>
6. 类型转换 - 数值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>类型转换-数值</title>
</head>
<body>
<script>
/*
1.使用Number()函数
- 字符串:
如果字符串是一个合法的数字,则会自动转换为对应的数值
反之,则是 Nan
如果字符串是空串或纯空格的字符串,转换后则是0
- 布尔值:
true转换为1,false转换为0
- null 转换为0
- undefined 转换为Nan
2.专门将字符串转换为数值的两个方法
- parseInt() 转换为整数
解析时会自左向右逐一读取字符串,直到读取完字符串中所有有效的整数
有些时候可以使用该方法对数值进行取整(不推荐)
- parseFloat() 转换为浮点数
解析时会自左向右逐一读取字符串,直到读取完字符串中所有有效的浮点数(小数)
*/
let a = '123'
a = 'abc' //Nan
a = '11px' //Nan
a = '' //0
a = ' ' //0
a = true //1
a = false //0
a = null //0
a = undefined //Nan
console.log(typeof a, a)
a = Number(a)
console.log(typeof a, a)
let b = '123'
b = '123px' //123
b = 456 //当传的值不是字符串而是数值时,parseInt()会先将456转换为'456',再进行整数转换
console.log(typeof b, b)
b = parseInt(b)
console.log(typeof b, b)
b = parseFloat(b)
console.log(typeof b, b)
</script>
</body>
</html>
7. 类型转换 - 布尔值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>类型转换-布尔值</title>
</head>
<body>
<script>
/*
1.使用Boolean()函数将其他类型转换为布尔值
- 数字:除了0和Nan转换后是false,其余都是true
- 字符串:空串转换为false,其余是true
- null、undefined转换为false
- 对象会转换为true
所有表示空的没有错误的值都会转换为false,即0,Nan,空串,null,undefined
*/
let a = 1 //true
a = -1 //true
a = 0 //false
a = NaN //false
a = Infinity //true
a = 'abc' //true
a = 'true' //true
a = 'false' //true
a = '' //false
a = ' ' //true
a = null //false
a = undefined //false
console.log(typeof a, a)
a = Boolean(a)
console.log(typeof a, a)
</script>
</body>
</html>
评论 (0)