数据类型(一)

100 阅读1分钟

数据类型

基本数据类型:number、string、null、undefined、boolean

引用数据类型:object

基本数据类型不可以改变

a.len = 'f'
console.log(a) // 'abcd'
a[1] = 'ccccccc'
console.log(a) // 'abcd'

如何查看数据类型:typeof

typeof所有的返回值都是字符串

typeof返回值:string、undefined、number、object、boolean、function 为什么没有null,因为null是object

typeof null // object

计算机语言中有两个值来表示无:undefined、null 为什么会这样的,因为最开始的时候是没有undefined这个值的,后来因为历史原因加入的undefined

那我们就理解一下undefined和null

相同点

  • 无论是undefined和null都只有一个值
  • 进行判断的时候都返回false
  • js中只有对象有方法,但是因为js特殊的原则,string、number、boolean也有自己的方法,唯一没有方法的就是undfined和null

不同点

  • null是个关键字undefined不是关键字
  • undefined的本质其实是windows上的一个属性。
  • undefined是个未初始化的东西。
  • null的时候是已经初始化为一个对象。
  • typeof的时候,null返回object,undefined返回的就是undefined。

所有类型的数据都可以被转为boolean类型

返回false的有 null、undefined、0、-0、''、NaN,除了这六种,其他的都会转为true。