一,数据类型
1.数据类型分类
- 基础类型:
number(数字)
:任意数值,例如:123,12.3
String(字符串)
:任意字符串,例如:‘123’,‘abc’
Blooean(布尔)
:有2个值,true/false
undefind(未定义)
:只有undefind值
null(空)
:只有null值
Symbol
:ES6新加的数据类型,表示独一无二的值
BigInt(任意精度整数)
:可以操作存储大整数据- 引用类型
object(对象)
:任意对象
Array(数组)
:特殊对象,内部数据有序,有下标
function(函数)
:特殊对象,可执行函数
2.数据类型判断
- typeof
能判断数值,字符串,布尔值,undefind,function,Symbol,bigint
不能判断null和Object,Object和Array
NaN会判断为number
typeof 1 // 'number'
typeof 'lily' // 'string'
typeof undefined // 'undefined'
typeof null // 'object'
typeof true // 'boolean'
typeof Symbol() // 'symbol'
typeof 123n // 'bigint'
typeof {name:'lily'} // 'object'
typeof [1,2,3] // 'object'
typeof function(){}// 'function'
typeof NaN // 'number'
- instanceof
判断对象的具体类型,返回值是Boolean值
语法:obj instanceof Type
功能:判断 obj 是不是 Type 类的实例,只可用来判断引用数据
const arr = [1, 2, 3]
console.log(arr instanceof Array) // true
console.log(arr instanceof Object) // true
const obj = { name: "lily" }
console.log(obj instanceof Object) // true
console.log(obj instanceof Array) // false
- 全等(===)
与某个固定值进行比较
y引用类型数据只有是相同地址才会相等
- Object.prototype.toString.call()
Object.prototype.toString.call(12); // '[object Number]'
Object.prototype.toString.call("csdn"); // '[object String]'
Object.prototype.toString.call(true); // '[object Boolean]'
Object.prototype.toString.call(undefined); // '[object Undefined]'
Object.prototype.toString.call(null); // '[object Null]'
Object.prototype.toString.call(Symbol()) // '[object Symbol]'
Object.prototype.toString.call(12345n) // '[object BigInt]'
Object.prototype.toString.call({name:'lily'}); // '[object Object]'
Object.prototype.toString.call([1,2,3]); // '[object Array]'
Object.prototype.toString.call(function(){}); // '[object Function]'
//可以封装函数获取类型
function getType(data) {
return Object.prototype.toString.call(data).slice(8, -1);
}
console.log(getType("csdn")) // String
二,数据,变量,内存
数据,变量,内存三者关系
- 内存是用来存储数据的空间
- 变量是内存的表示
1.数据
- 内存中所有操作的目标:数据
算术运算
逻辑运算
赋值
运行函数
2.变量
- 可变化的量,由变量名和变量值组成
- 每个变量都对应一小块内存,变量名用来查找对应的内存,变量值就是内存中保存的数据
3.内存
- 内存是用来存储数据的空间
- 内存分类
栈
:全局变量/局部变量
堆
:对象