JavaScript 数据类型及object类型介绍

1、数据类型

在这里插入图片描述

2、ES 语言介绍及相关示例

ES 是动态语言,弱类型语言。

  • 动态语言:声明变量时,不需指定具体类型
  • 静态语言:声明变量时,需要指定具体类型
  • 弱类型语言:允许隐式类型转换
  • 强类型语言:不允许隐式类型转换

备注:强弱类型语言,可以通过字符串和数字相加进行判断

// Input
console.log('a' + 1 + 'b')
// Output
 (下午3:43:04)
a1b
Info: End process (下午3:43:05)

虽然事先声明了变量,但是变量可以重新赋值任何类型。

/*
说明:
1、遇到字符串,加号就是拼接字符串,所有非字符串隐式转换为字符串
2、如果没有字符串,加号把其它所有类型都当数字处理,非数字类型隐式转换为数字
3、undefined 特殊,因为它都没有定义值,所以转换数字失败得到一个特殊值 NaN
4、如果运算符是逻辑运算符,短路符,返回的就是短路时的类型,没有隐式转换
5、除非你十分明确,否则不要依赖隐式转换。写代码的时候,往往为了程序的健壮,请显式转换
*/
console.log("=====String=====")
console.log(1, a = 3 + 'Jiao', typeof(a))
console.log(2, a = null + 'Jiao', typeof(a))
console.log(3, a = undefined + 'Jiao', typeof(a))
console.log(4, a = true + 'Jiao', typeof(a))

console.log("=====Number=====")
console.log(5, a = 3 + 8, typeof(a))
console.log(6, a = null + 8, typeof(a))
console.log(7, a = undefined + 8, typeof(a))
console.log(8, a = true + 8, typeof(a))

console.log(9, Number.isNaN(NaN))

console.log("=====Bool=====")
console.log(10, a = null + true, typeof(a)) 
console.log(11, a = null + false, typeof(a))
console.log(12, a = undefined + true, typeof(a))
console.log(13, a = undefined + false, typeof(a))
console.log(14, a = null & true, typeof(a))
console.log(15, a = undefined & true, typeof(a))
console.log(16, a = null & false, typeof(a))
console.log(16, a = undefined & false, typeof(a))
console.log(18, a = true | false, typeof(a))
console.log(19, a = null | undefined, typeof(a))
console.log(20, a = null && true, typeof(a))
console.log(21, a = false && null, typeof(a))
console.log(22, a = false && 'Jiao', typeof(a))
console.log(23, a = true && 'Jiao', typeof(a))
console.log(24, a = true && '', typeof(a))
console.log(25, a = NaN + 1000, typeof(a))
 (下午12:22:04)
=====String=====
1 '3Jiao' 'string'
2 'nullJiao' 'string'
3 'undefinedJiao' 'string'
4 'trueJiao' 'string'
=====Number=====
5 11 'number'
6 8 'number'
7 NaN 'number'
8 9 'number'
9 true
=====Bool=====
10 1 'number'
11 0 'number'
12 NaN 'number'
13 NaN 'number'
14 0 'number'
15 0 'number'
16 0 'number'
16 0 'number'
18 1 'number'
19 0 'number'
20 null 'object'
21 false 'boolean'
22 false 'boolean'
23 'Jiao' 'string'
24 '' 'string'
25 NaN 'number'
Info: End process (下午12:22:04)

3、object类型

// object

b = 1000
c = 2000

let a = {b, c, d:3000}
console.log(a.b, a.c, a.d, a, typeof(a))
(下午3:48:49)
1000 2000 3000 { b: 1000, c: 2000, d: 3000 } 'object'
Info: End process (下午3:48:50)

4、逻辑运算符

// and 为 &&
// or 为 ||
// [] {} 等效为 true

console.log(1, a = true && '', typeof(a))
console.log(2, a = '' && 123, typeof(a))
console.log(3, a = true && 123, typeof(a))
console.log(4, a = [] && '123', typeof(a)) 
console.log(5, a = {} && '123', typeof(a))
console.log(6, a = false && '123', typeof(a))
console.log(7, a = false || '123', typeof(a))
console.log(8, a = '123' || false, typeof(a))
console.log(9, a = false || true, typeof(a))
 (下午4:10:58)
1 '' 'string'
2 '' 'string'
3 123 'number'
4 '123' 'string'
5 '123' 'string'
6 false 'boolean'
7 '123' 'string'
8 '123' 'string'
9 true 'boolean'
Info: End process (下午4:10:58)

猜你喜欢

转载自blog.csdn.net/weixin_44983653/article/details/107358029