发布于 2026-01-06 4 阅读
0

10 个 JavaScript 面试题

10 个 JavaScript 面试题

注意:每个问题后面都附有输出结果和简要解释。请自行承担滚动浏览的风险。

Q1.

var msg = "hello";

if (true) {
  var msg = "welcome";
}
console.log(msg);

// ----

let msg2 = "hello";

if (true) {
  let msg2 = "welcome";
}
console.log(msg2);
Enter fullscreen mode Exit fullscreen mode

输出

welcome
hello
Enter fullscreen mode Exit fullscreen mode

解释

var由于 `this` 是函数作用域的,因此,当msg它在代码块内声明时if,它会覆盖全局作用域中的 `this` 。而 `this` 是代码块作用域的,所以msg不会发生这种情况。let

Q2.

for (var i = 0; i < 5; i++) {
  setTimeout(function () {
    console.log(i);
  }, 1000);
}
Enter fullscreen mode Exit fullscreen mode

输出

5
5
5
5
5
Enter fullscreen mode Exit fullscreen mode

解释

由于var该变量的作用域是函数式的,因此循环结束后i变量的值保持不变。回调函数每次都会获取到相同的值。5setTimeout

解决方案

  • 转换varlet为每次迭代创建一个作用域。
for (let i = 0; i < 5; i++) {
  setTimeout(function () {
    console.log(i);
  }, 1000);
}
Enter fullscreen mode Exit fullscreen mode
  • 将值封装setTimeout在一个匿名函数中。将其i作为参数传递会将其作用域限定在匿名函数内,因此值不会丢失。
for (var i = 0; i < 5; i++) {
  (function (x) {
    setTimeout(function () {
      console.log(x);
    }, 1000);
  })(i);
}
Enter fullscreen mode Exit fullscreen mode

Q3.

const obj = {
  ["foo_" + (() => 1)()]: "Hello",
};
console.log(obj);
Enter fullscreen mode Exit fullscreen mode

输出

{ foo_1: 'Hello' }
Enter fullscreen mode Exit fullscreen mode

第四季度。

console.log([1,2,3] + [4,5,6]);
Enter fullscreen mode Exit fullscreen mode

输出

1,2,34,5,6
Enter fullscreen mode Exit fullscreen mode

解释

String([1,2,3]);"1,2,3"

因此,"1,2,3" + "4,5,6""1,2,34,5,6"

Q5. 执行顺序是什么?

console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
console.log('4');
Enter fullscreen mode Exit fullscreen mode

输出

1
4
3
2
Enter fullscreen mode Exit fullscreen mode

解释

事件循环的优先级顺序为:调用栈 > 微任务 > 宏任务

所有同步代码首先执行。

因此,日志14

接下来,有一个PromisesetTimeout

Promise 回调函数存储在 [此处] microtask queue,setTimeout 回调函数存储在 [此处]。macrotask queue

microtask优先级更高macrotask。因此,它会记录3后续内容。2

Q6.

console.log(typeof typeof 1);
Enter fullscreen mode Exit fullscreen mode

输出

string
Enter fullscreen mode Exit fullscreen mode

解释

从右到左进行评估

  1. typeof 1返回号码
  2. typeof 'number'返回字符串

Q7.

console.log(Math.max() < Math.min());
Enter fullscreen mode Exit fullscreen mode

解决方案

true
Enter fullscreen mode Exit fullscreen mode

解释

默认值为 Math.max() &  默认 -Infinity 值为 Math.min()Infinity

因此, -Infinity < Infinitytrue

Q8.

function func() {
  return foo;

  foo = 1;
  function foo() {}
  var foo = "hello";
}

console.log(typeof func());
Enter fullscreen mode Exit fullscreen mode

输出

function
Enter fullscreen mode Exit fullscreen mode

解释

由于经过一轮解析(提升),代码看起来像这样

function func() {
  var foo; // hoisted
  function foo() {} // hoisted
  return foo;

  foo = 1;
  foo = "hello";
}

console.log(typeof func());
Enter fullscreen mode Exit fullscreen mode

因此,foo 的最后一个可用值为function

Q9.

console.log(3 > 2 > 1);
Enter fullscreen mode Exit fullscreen mode

输出

false
Enter fullscreen mode Exit fullscreen mode

解释

它从左到右开始,
所以3 > 2等于true

true > 1等价于,1 > 1false

Q10.

function fn() {
  return this.str;
}

var str = "global";

var obj = {
  str: "obj",
};

console.log(fn(), fn.call(obj));
Enter fullscreen mode Exit fullscreen mode

输出

global obj
Enter fullscreen mode Exit fullscreen mode

解释

执行时,fn()值为thisiswindowwindow.stris global

.call()分配thisobj并且obj.strobj

注意此解决方案在模式下有效non-strict


感谢阅读💙

关注@codedrops.tech获取每日更新

。InstagramTwitterFacebook

微学习 ● Web 开发 ● Javascript ● MERN 技术栈 ● Javascript

codesdrops.tech

文章来源:https://dev.to/ml318097/10-javascript-interview-questions-3n6c