JavaScript速查表
JavaScript核心语法和常用API快速参考。
变量声明
var x = 1
函数作用域(不推荐)
let y = 2
块级作用域,可重新赋值
const z = 3
块级作用域,常量
数据类型
typeof "hello"
"string"
typeof 42
"number"
typeof true
"boolean"
typeof undefined
"undefined"
typeof null
"object"
typeof {}
"object"
数组方法
arr.push(val) // 尾部添加
arr.pop() // 尾部删除
arr.unshift(val) // 头部添加
arr.shift() // 头部删除
arr.map(fn) // 映射新数组
arr.filter(fn) // 过滤数组
arr.reduce(fn) // 累计计算
arr.find(fn) // 查找元素
arr.includes(v) // 是否包含函数
function f() {}
函数声明
const f = () => {}
箭头函数
async function f() {}
异步函数
function* g() {}
生成器函数
异步操作
fetch(url)
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.error(err))
// async/await
const res = await fetch(url)
const data = await res.json()