Home >Web Front-end >JS Tutorial >Share 12 interview questions to see if you understand JavaScript!

Share 12 interview questions to see if you understand JavaScript!

青灯夜游
青灯夜游forward
2022-09-14 19:36:441790browse

Do you know JavaScript? The following article will share with you 12 JavaScript interview questions. Try these 12 interview questions and see if you can answer them all correctly!

Share 12 interview questions to see if you understand JavaScript!

JavaScript is a basic technology that every front-end developer should master, but many times, you may not fully understand JavaScript.

There are two very simple ways to test a person's technical level, look at the code he writes, or let him look at the code written by others.

I have summarized some code questions that can test your understanding of JavaScript. You can try it and see if you can answer them all correctly. If you answer all the questions correctly, you will be considered to know some JavaScript.

First question

Try to guess its output:

const person = { name: '代码与野兽' }
Object.defineProperty(person, 'age', { value: 18 })

console.log(person.age)
console.log(Object.keys(person))

Output:

18
['name']

Analysis:
Many people easily mistake the second output because properties defined using defineProperty are not enumerable by default.

Second Question

Try to guess its output:

const name = '代码与野兽'
age = 18

console.log(delete name)
console.log(delete age)
console.log(typeof age)

Output:

false
true
"undefined"

Analysis:
The first false is because delete can only delete attributes on the object, and name is not an attribute, so the deletion fails.
The second true is because we do not use any declaration to create the variable. It will be regarded as a global variable and mounted on the window object, which is equivalent to delete window.age, so the deletion is successful.
The third undefined is because age was deleted.

The third question

Try to guess its output:

let person = { name: '代码与野兽' }
const members = [person]
person = null
console.log(members)

Output:

[{
  name: "代码与野兽"
}]

Analysis:
Many people will think that the output result should be [null], but we just set a new reference to the person variable, and the previous reference is still in members.
To put it simply, { name: 'Code and Beast' } This object exists in a certain memory space, assuming its address is X201. Its logic is roughly as follows:

let person = X201
const members = [X201]
persion = null

Question 4

Try to guess Its output:

function SuperHero() {
  this.make = '代码与野兽'
  return { make: '野兽与代码'}
}

const mySuperhero = new SuperHero()
console.log(mySuperhero)

Output:

{
  make: "野兽与代码"
}

Parsing:
If the constructor finally returns an object, all previously set properties will be invalid.

Question 5

Try to guess its output:

const name = '代码与野兽'
console.log(name.padStart(14))
console.log(name.padStart(2))

Output:

"         代码与野兽"
"代码与野兽"

Analysis:
padStart method can fill spaces at the beginning of the string.
The parameter is the total length of the new string. If this length is shorter than the original string length, it will not be filled.

Question 6

Try to guess its output:

console.log(parseInt("7"))
console.log(parseInt("7*6"))
console.log(parseInt("7Din"))

Output:

7
7
7

Analysis:
If the parameter of parseInt is a combination of string and number, then it will check from the beginning until it encounters the position with the wrong data type. If there is a valid position before the position with the wrong data type Number, it will return a number.

Question 7

Try to guess its output:

[1, 2, 3, 4].reduce((x, y) => console.log(x, y))

Output:

1
2
undefined
3
undefined
4

Analysis:
If we do not pass the initial value to reduce, then x will be the first value of the array and y will be the second value of the array.

Question 8

Try to guess its output:

function getUserInfo(one, two, three) {
  console.log(one)
  console.log(two)
  console.log(three)
}

const superHero = '代码与野兽'
const age = 1000

getUserInfo`${superHero} 是 ${age} 岁`
getUserInfo`hello`

Output:

["", " 是 ", " 岁"]
"代码与野兽"
1000
["hello"]
undefined
undefined

Analysis:
When we use template string syntax to call a function, the first parameter is always a split string array. The remaining parameters are the values ​​of the template expression.

Question 9

Try to guess its output:

(() => {
  let x, y;
  try {
    throw new Error()
  } catch (x) {
    (x = 1), (y = 2);
    console.log(x)
  }
  console.log(x)
  console.log(y)
})()

Output:

1
undefined
2

Analysis:
When accessing x in catch, the parameters are accessed, not the external variable x.

Question 10

Try to guess its output:

class Clazz {}

console.log(typeof Clazz)

Output:

"function"

解析:
在 JavaScript 中,Class 也是 function。

第十一题

尝试推测它的输出:

const arr = [7, 1, 4, 3, 2];
for (const elem of arr) {
  setTimeout(() => console.log(elem), elem);
}

输出:

1
2
3
4
7

解析:
没什么好解释的......

第十二题

尝试推测它的输出:

const foo = { bar: 1 };
with(foo) {
  var bar = 2
};
console.log(foo.bar);

输出:

2

解析:

with 的对象会作为 global 对象。在 with 使用 var 等价于 window.[xxx]。而这时 foo 就是那个 window。

【相关推荐:javascript学习教程

The above is the detailed content of Share 12 interview questions to see if you understand JavaScript!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete