JaveScript数据类型相关知识点及面试题总结

175 阅读5分钟

第一部分介绍JS的基础数据类型

1. String

let str = 'sun';
console.log(typeof str) // String

2. Number

  • JS 的 number 类型是浮点类型的,0.1 + 0.2 !== 0.3,所以不要计算某个特定的浮点数值,这是使用IEEE754的数值的浮点计算的通病

  • string 类型是不可变的,无论你在 string 类型上调用何种方法,都不会对值有改变

let n = 123;
console.log(typeof n) // Number

console.log(0.1+0.2) // 0.30000000000000004

3. Boolean

let b = true;
console.log(typeof b) // Boolean

4. Undefined

  • 通常已定义但未被初始化的变量的值是undefined
let b = undefined;
console.log(typeof b) // undefined

5. Null

  • null在本质上是一个空对象指针,所以typeof null会返回object通常被认为是一种特殊的object
let b = null;
console.log(typeof b) // Object

6. Symbol

  • Symbol是ES6新增的一种基本类型
let b = Symbol(12);
console.log(typeof b) // Symbol

第二部分介绍的复杂数据类型Object

在 JS 中,除了原始类型那么其他的都是对象类型了。对象类型和原始类型不同的是,原始类型存储的是值,对象类型存储的是地址(指针)。当你创建了一个对象类型的时候,计算机会在内存中帮我们开辟一个空间来存放值,但是我们需要找到这个空间,这个空间会拥有一个地址(指针)

let b = {};
console.log(typeof b) // object

第三部分介绍类型判断操作符typeof

1. 基本使用typeof a或者typeof(a)

  • 因为typeof是操作符所以加不加括号都可以使用
  • 从下面的示例我们可以看出typeof在判断nullarrayobject以及函数实例(new + 函数)时,得到的都是object,当我们想要判断一个对象来自哪个原型的时候这时候就没办法使用typeof
  • 所以我们可以总结typeof的返回类型:number string undefined object symbol function
typeof 1 // 'number'
typeof '1' // 'string'
typeof undefined // 'undefined'
typeof true // 'boolean'
typeof Symbol() // 'symbol'
typeof Stypeof [] // 'object'
typeof {} // 'object'
typeof console.log // 'function'

第四部分介绍类型判断函数instanceof

1. instance中文翻译为实例,因此instanceof的含义就不言而喻,判断该对象是谁的实例,同时我们也就知道instanceof是对象运算符,这里的实例就牵扯到了对象的继承,它的判断就是根据原型链进行搜寻,在对象obj1的原型链上如果存在另一个对象obj,那么obj1.instanceof(obj)就会返回true

const Person = function() {}
const p1 = new Person()
p1 instanceof Person // true

var str = 'hello world'
str instanceof String // false

var str1 = new String('hello world')
str1 instanceof String // true

2.对于原始类型来说,你想直接通过 instanceof 来判断类型是不行的,当然我们还是有办法让 instanceof 判断原始类型的

class PrimitiveString {
  static [Symbol.hasInstance](x) {
    return typeof x === 'string'
  }
}
console.log('hello world' instanceof PrimitiveString) // true

第五部分介绍类型转换

1. 转为String

原始类型原始值目标类型转换结果
Nubmer

0

123

NaN

String

'0'

'123'

'NaN'

Boolean

true

false

String

'true'

'false'

nullnullString'null'
undefinedundefinedString'undefined'
symbol

Symbol(12)

String'Symbol(12)'
object

a = {

name: 'sun'

}

String'[object Object]'
var a = NaN;
console.log(String(a)); // NaN
console.log(typeof(String(a))); // string
a = true;
console.log(String(a)); // true
console.log(typeof(String(a))); // string
a = null;
console.log(String(a)); // null
console.log(typeof(String(a))); // string
a = undefined; 
console.log(String(a)); // undefined
console.log(typeof(String(a))); // string
a = Symbol(12)
console.log(String(a)); // Symbol(12)
console.log(typeof(String(a))); // string
a = {
    name: 'sun'
}
console.log(String(a)); // [object Object]
console.log(typeof(String(a))); // string

2. 转为Number

原始类型原始值目标类型转换结果
String

''

'123'

'abc'

Number

0

123

NaN

Boolean

true

false

Number

1

0

nullnullNumber0
undefinedundefinedNumber0
symbol

Symbol(12)

Number抛出异常不可将symbo转为number
object

a = {

name: 'sun'

}

Number'[object Object]'
var a = '';
console.log(Number(a)); // 0
console.log(typeof(Number(a))); // number

a = '123';
console.log(Number(a)); // 123
console.log(typeof(Number(a))); // number

a = 'abc';
console.log(Number(a)); // NaN
console.log(typeof(Number(a))); // number

a = true;
console.log(Number(a)); // 1
console.log(typeof(Number(a))); // number

a = null;
console.log(Number(a)); // 0
console.log(typeof(Number(a))); // number

a = undefined; 
console.log(Number(a)); // NaN
console.log(typeof(Number(a))); // number

a = Symbol(12)
// console.log(Number(a)); // 抛出异常

a = {
    name: 'sun'
}
console.log(Number(a)); // NaN
console.log(typeof(Number(a))); // number

3. 转为Boolean

原始类型原始值目标类型转换结果
String

''

'123'

'true'

Boolean

false

true

true

Number

NaN

0

123

Boolean

false

false

true

nullnullBooleanfalse
undefinedundefinedBooleanfalse
symbol

Symbol(12)

Booleantrue
object

a = {}

a = {

name: 'sun'

}

Boolean

true

true

var a = '';
console.log(Boolean(a)); // false
console.log(typeof(Boolean(a))); // boolean

a = '123';
console.log(Boolean(a)); // true
console.log(typeof(Boolean(a))); // boolean

a = 'abc';
console.log(Boolean(a)); // true
console.log(typeof(Boolean(a))); // boolean

a = 'true';
console.log(Boolean(a)); // true
console.log(typeof(Boolean(a))); // boolean

a = 0;
console.log(Boolean(a)); // false
console.log(typeof(Boolean(a))); // boolean

a = null;
console.log(Boolean(a)); // false
console.log(typeof(Boolean(a))); // boolean

a = undefined; 
console.log(Boolean(a)); // false
console.log(typeof(Boolean(a))); // boolean

a = Symbol(12)
console.log(Boolean(a)); // true
console.log(typeof(Boolean(a))); // boolean


a = {
    name: 'sun'
}
console.log(Boolean(a)); // true
console.log(typeof(Boolean(a))); // boolean

a = {}
console.log(Boolean(a)); // true
console.log(typeof(Boolean(a))); // boolean

第六部分介绍类型相关常见面试题

1. 原始类型有哪几种?null 是对象嘛?

2.对象类型和原始类型的不同之处?函数参数是对象会发生什么问题?

3.typeof和instanceof的区别

4.讲下不同类型之间的转换

5.一下代码会输出什么?

function test(person) {
  person.age = 26
  person = {
    name: 'yyy',
    age: 30
  }

  return person
}
const p1 = {
  name: 'yck',
  age: 25
}
const p2 = test(p1)
console.log(p1) // -> ?
console.log(p2) // -> ?