JavaScript 基础核心知识点闯关练习
关卡1:变量与数据类型
变量声明
let a = 5;
const b = "Hello";
var c = true;
数据类型检测
console.log(typeof null); // "object"
console.log(typeof undefined); // "undefined"
console.log(typeof []); // "object"
关卡2:运算符
比较运算符
console.log(1 == '1'); // true
console.log(1 === '1'); // false
逻辑运算符
console.log(0 || "default"); // "default"
console.log(null ?? "fallback"); // "fallback"
关卡3:函数
箭头函数
const sum = (x, y) => x + y;
console.log(sum(3, 4)); // 7
闭包
function createCounter() {
let count = 0;
return () => ++count;
}
const counter = createCounter();
console.log(counter()); // 1
关卡4:作用域
{
let blockScoped = "inside";
var functionScoped = "inside";
}
console.log(functionScoped); // "inside"
console.log(blockScoped); // ReferenceError
关卡5:异步处理
Promise
new Promise((resolve) => {
setTimeout(() => resolve("Done!"), 1000);
}).then(console.log); // 1秒后输出"Done!"
Async/Await
async function fetchData() {
const res = await fetch('https://api.example.com');
return res.json();
}
闯关提示:
- 所有代码可直接在浏览器控制台测试
- 注意区分let/const/var作用域差异
- 异步操作需理解事件循环机制
- 建议使用严格模式:"use strict";




