数据类型
基本类型
number
string
boolean
null
undefined
symbol
number
const num = 10
const num8 = 017
const num16 = 0x12ee
const floatNum = 0.1
const ENum = 3.1415926E7
const ENum2 = 3.1415926e8
console.log(0 / 0)
string
const str = 'hello world'
const firstName = 'hu'
const str2 = `hello ${firstName}`
boolean
console.log(1 == true)
console.log(0 == false)
console.log('' == false)
console.log(null == false)
console.log(undefined == false)
console.log(NaN == false)
null
const n = null
console.log(typeof n)
undefined
let u
console.log(typeof u)
symbol
const s = Symbol('foo')
console.log(typeof s)
复杂类型
object
const obj = {
name: 'hu',
sayHi: function () {
console.log('hi')
},
'age':5,
3: 'three'
}
obj.name
obj['name']
obj.sayHi()
obj['sayHi']()
obj.age
obj['age']
obj[3]
obj['3']
function
function add (a, b) {
return a + b
}
const add2 = function (a, b) {
return a + b
}
const add3 = (a, b) => {
return a + b
}
array
const arr = [1, [2],{name: 'hu'}]
存储的区别
- 基本类型存储的是值,基本数据类型存储在栈中
- 复杂类型存储的是地址,复杂数据类型存储在堆中
- 基本类型
let a = 1
let b = a
a = 2
console.log(b)
- 复杂类型 是对地址的引用,修改其中一个,另一个也会改变
let a = {name: 'hu'}
let b = a
a.name = 'huhu'
console.log(b.name)