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!

Sep 14, 2022 pm 07:36 PM
javascript

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))
Copy after login

Output:

18
['name']
Copy after login

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)
Copy after login

Output:

false
true
"undefined"
Copy after login

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)
Copy after login

Output:

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

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
Copy after login

Question 4

Try to guess Its output:

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

const mySuperhero = new SuperHero()
console.log(mySuperhero)
Copy after login

Output:

{
  make: "野兽与代码"
}
Copy after login

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))
Copy after login

Output:

"         代码与野兽"
"代码与野兽"
Copy after login

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"))
Copy after login

Output:

7
7
7
Copy after login

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))
Copy after login

Output:

1
2
undefined
3
undefined
4
Copy after login

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`
Copy after login

Output:

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

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)
})()
Copy after login

Output:

1
undefined
2
Copy after login

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)
Copy after login

Output:

"function"
Copy after login

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

第十一题

尝试推测它的输出:

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

输出:

1
2
3
4
7
Copy after login

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

第十二题

尝试推测它的输出:

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

输出:

2
Copy after login

解析:

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles