坊间传闻,现在的前端面试者的简历都写精通JS,那就拿这些题考考,若不能全答对,就不要说精通了,这只是中高级前端的基础题而已。
JS选择题
1.请问JS中的基本数据类型有几种?( )
- A.5
- B.6
- C.7
2.下面代码的输出是什么?( )
function sayHi() { console.log(name); console.log(age); var name = "TJH"; let age = 24; }
- A: TJH 和 undefined
- B: TJH 和 ReferenceError
- C: ReferenceError 和 24
- D: undefined 和 ReferenceError
3.下面代码的输出是什么?( )
for (var i = 0; i < 3; i++) { setTimeout(function () { console.log(i); }, 1); } for (let i = 0; i < 3; i++) { setTimeout(function () { console.log(i); }, 1); }
- A: 0 1 2 and 0 1 2
- B: 0 1 2 and 3 3 3
- C: 3 3 3 and 0 1 2
4.下面代码的输出是什么?( )
let a = 666; let b = new Number(666); let c = 666; console.log(a == b); console.log(a === b); console.log(b === c);
- A: true false true
- B: false false true
- C: true false false
- D: false true true
5.下面代码的输出是什么?( )
const a = {}; const b = { key: "b" }; const c = { key: "c" }; a[b] = 123; a[c] = 456; console.log(a[b]);
- A: 123
- B: 456
- C: undefined
- D: ReferenceError
6.下面代码的输出是什么?( )
const numbers = [1, 2, 3]; numbers[10] = 11; console.log(numbers);
- A: [1, 2, 3, 7 x null, 11]
- B: [1, 2, 3, 11]
- C: [1, 2, 3, 7 x empty, 11]
- D: SyntaxError
7.下面代码的输出是什么?( )
let number = 0; console.log(number++); console.log(++number); console.log(number);
- A: 1 1 2
- B: 1 2 2
- C: 0 2 2
- D: 0 1 2
8.下面代码的输出是什么?( )
let obj1 = { name: 'obj1_name', print: function () { return () => console.log(this.name); } } let obj2 = { name: 'obj2_name' }; obj1.print()(); obj1.print().call(obj2); obj1.print.call(obj2)();
- A: obj1_name obj2_name obj2_name
- B: obj2_name obj1_name obj2_name
- C: obj1_name obj1_name obj2_name
9.下面代码的输出是什么?( )
const obj = { 1: "a", 2: "b", 3: "c" }; const set = new Set([1, 2, 3, 4, 5]); obj.hasOwnProperty("1"); obj.hasOwnProperty(1); set.has("1"); set.has(1);
- A: false true false true
- B: false true true true
- C: true true false true
- D: true true true true
10.下面代码的输出是什么?( )
function Foo() { getName = function () { console.log(1); }; return this; } Foo.getName = function () { console.log(2); } Foo.prototype.getName = function () { console.log(3); }; var getName = function () { console.log(4); }; function getName() { console.log(5); } Foo.getName(); getName(); Foo().getName(); getName(); new Foo.getName(); new Foo().getName(); new new Foo().getName();
- A.4 2 1 1 2 3 3
- B.2 1 4 1 2 3 3
- C.2 4 1 1 3 2 3
- D.2 4 1 1 2 3 3
11.下面代码的输出是什么?( )
async function async1() { console.log('async1 start'); await async2(); console.log('async1 end'); } async function async2() { console.log('async2'); } console.log('script start'); setTimeout(function () { console.log('setTimeout0'); }, 0); setTimeout(function () { console.log('setTimeout3'); }, 0); setImmediate(() => console.log('setImmediate')); process.nextTick(() => console.log('nextTick')); async1(); new Promise(function (resolve) { console.log('promise1'); resolve(); console.log('promise2'); }).then(function () { console.log('promise3'); }); console.log('script end');
- A: script start - async2 start - async1 - promise1 - promise2 - script end - nextTick - async1 end - promise3 - setTimeout0 - setImmediate - setTimeout3
- B: script start - async1 start - async2 - promise2 - promise1 - script end - nextTick - async1 end - promise3 - setTimeout0 - setImmediate - setTimeout3
- C: script start - async1 start - async2 - promise1 - promise2 - script end - nextTick - async1 end - promise3 - setTimeout3 - setImmeidate - setTimeout0
- D: script start - async1 start - async2 - promise1 - promise2 - script end - nextTick - async1 end - promise3 - setTimeout0- setImmeidate - setTimeout3
参考答案
CDCCBCCCCDD