Skip to content

JS面试思考:2基础知识点

先看几道题:

  1. js总使用typeof能得到的变量类型? 考点:js变量类型

  2. 何时用 === 何时用 == ? 考点:强制类型转换

  3. js有哪些内置函数

  4. js变量按照存储方式区分为哪些类型,并描述其特点

  5. 如何理解JSON


变量类型与变量计算

*值类型

*引用类型 - 对象、数组、函数

typeof 运算符

变量计算 - 强制类型转换

  • 字符串拼接
js
var a = 100 + 10 // 110
var b = 100 + '10' // 10010
  • == 运算符
js
100 == '100' // true
0 == ''  // true
null == undefined  // true
  • if语句
js
var a = true
if (a) {
  // ...
}
var b = 100
if (b) {
  // ...
}
var c = ''
if (c) {
  // ...
}
  • 逻辑运算
js
console.log(10 && 0) // 0
console.log('' || 'abc') // 'abc'
console.log(!window.abc) // true
//判断一个变量会被当作true还是false
var a = 100
console.log(!!a)

解题

  1. typeof类型
js
typeof undefined // undefined
typeof 'abc'  // string
typeof 123  // number
typeof true // boolean
typeof {}  // object
typeof []  // object
typeof null // object
typeof console.log  // function
  1. 何时使用 === 和 ==

    if (obj.a == null) { // ... }

这里相当于 obj.a === null || obj.a === undefined 的简写形式 这是jQuery源码中推荐的写法

  1. JS中的内置函数 - 数据封装类对象

Object Array Boolean Number String Function Date RegExp Error

  1. js变量按照存储方式区分为哪些类型,并描述其特点
js
/* 值类型 */
var a = 10
var b = a
a = 11
console.log(b)  // 11
/* 引用类型 */
var obj1 = { x: 100 }
var obj2 = obj1
obj1.x = 200
console.log(obj2.x) // 200
  1. 如何理解JSON
js
// - JSON是一个JavaScript对象,有自己的api
// - 数据格式
JSON.stringify({a: 10, b: 20})
JSON.parse(`{"a": 10, "b": 20}`)

共 20 个模块,1301 篇 Markdown 文档。