Table of Contents
What is the closure in Javascript?
What is DOM in Javascript
What is Promise in Javascript
What is the prototype in Javascript?
What is variable promotion in Javascript
Javascript中的对象是什么
Javascript 中的函数是什么
Javascript中的纯函数是什么
Javascript中的构造函数是什么
Javascript类是什么?
总结
Home Web Front-end JS Tutorial 10 JavaScript interview questions frequently asked by developers (with answer analysis)

10 JavaScript interview questions frequently asked by developers (with answer analysis)

Apr 26, 2021 pm 06:51 PM
javascript front end Interview questions

This article introduces the 10 most frequently asked JavaScript interview questions for beginners. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to beginners.

10 JavaScript interview questions frequently asked by developers (with answer analysis)

In this article, I have collected the 10 most frequently asked questions about Javascript and their answers.

Most of these 10 questions involve the basics of Javascript, so if you are just starting to learn JS, it is best to understand and master them.

This 10 question involves closures, promise, variable promotion, classes, etc. in JS. While these aren't very difficult to learn, it's good to know the answers because some of them are often asked in interviews. [Recommended learning: javascript advanced tutorial]

What is the closure in Javascript?

A closure is a combination of functions enclosed together, where the inner function can access its variables and the variables of the outer function.

The simplest way to explain it is the above example:

function outer() {
  var name = 'Maria';
  function inner() {
    console.log(name);
  }
  inner();
}
outer();
// 'Maria'
Copy after login

In the above code, you can see that the inner() function can access its parent function variablename. Therefore, if the outer() function is called, then the inner() function's console.log() will return the value of name ##Maria.

Internal functions can access external function parameter objects, but internal function parameters are the same as external ones, so the internal parameter object will overwrite the external parameter object. It looks like this:

function outer(a, b) {
  const inner = (a, b) => console.log(a, b);
  inner(1, 2);
}
outer('Alice', 'Mark');
// returns 1, 2
Copy after login

The main reason why we use closures is to return functions that can return other functions.

What is DOM in Javascript

DOM is the Document Object Model, which is an object-oriented representation of the website and can be modified using Javascript.

Use JS to manipulate DOM elements, such as color, position, and size. To select specific elements of the page, Javascript provides some methods:

  • getElementById() - Select an element by id attribute
  • getElementsByName() - Select an element via the name attribute
  • getElementsByTagName() - Select all elements of the selected tag,
  • getElementsbyClassName() - Select all elements with a specific class name
* 

querySelector() - Select elements through CSS selectors.

Javascript also provides other methods of operating elements, not just getting elements, such as

appendChild() or innerHTML().

What is Promise in Javascript

Promise is a solution for asynchronous programming that can replace traditional solutions-callback functions and events. ES6 unifies usage and provides

Promise objects natively. As an object, Promise has the following two characteristics: (1) The state of the object is not affected by the outside world. (2) Once the state changes, it will not change. That is to say, Promise has only one state at any time.

Promise has three states, namely:

Pending (in progress), Resolved (completed), Rejected (failed). Promise starts from the Pending state. If it succeeds, it goes to the success state and executes the resolve callback function; if it fails, it goes to the failure state and executes the reject callback function. .

If the Promise is resolved, we can call the

then() method and use the return value to perform the operation. If rejected, we can use the catch() method to handle the error.

Other ways to handle asynchronous programming are

async/await and callbacks.

What is the prototype in Javascript?

Prototype usually refers to the two prototype objects

prototype and __proto__ , the former is called an explicit prototype object, and the latter is called an implicit prototype object.

Javascript objects inherit methods and properties from the prototype, and

Object.prototype is at the top of the inheritance chain. The Javascript prototype keyword can also be used to add new values ​​and methods to the constructor.

Let’s take a look at the example:

function Animal(name, kind, age) {
  this.name = name;
  this.kind = kind;
  this.age = age;
}

Animal.prototype.ownerName('Mark');
Copy after login

As you can see, by using the prototype, we can add the

ownerName attribute to the Animal() constructor middle.

What is variable promotion in Javascript

Promotion is a mechanism that promotes all declared variables and functions to their local scope If variables and functions are placed in the global scope, they will be promoted to the top of the global scope.

In Javascript, you can declare a variable after it is used.

Promotion is used to avoid undefined errors caused by executing a variable or function before it is defined.

name = 'Ted';
console.log(name);
var name;
// 'Ted'


var name;
name = 'Ted';
console.log(name);
// 'Ted';
Copy after login

使用 var 声明的变量,如果没有赋值,则默认会被初始化为 undefined, letconst 则不会。另外,需要注意的是,在声明const时,必须同时初始化它,因为后面不可在更改它。

Javascript中的对象是什么

对象只是一种特殊的数据。对象拥有属性和方法。JavaScript 中的所有事物都是对象,如:字符串、数值、数组、函数等。

对象的属性:反映该对象某些特定的性质的,如:字符串的长度、图像的长宽等;

对象的方法:能够在对象上执行的动作。例如,表单的“提交”(Submit),时间的“获取”(getYear)等;

属性只是简单的值,而方法是可以在对象上执行的操作。

var student = {
  firstName: 'Alice',
  lastName: 'Jones',
  age: 21,
  sayHi: () => {
    return 'Hi, I am ' + this.firstName;
  }
}
Copy after login

在上面的代码中,你可以看到Student对象,其中包含三个属性和一个方法。

Javascript 中的函数是什么

在javascript中函数是一段可以被执行或调用任意次数的JavasScript代码,在数据类型中属于"function"。函数也拥有属性和方法,因此函数也是对象。

在Javascript中函数定义函数声明或函数表达式由关键字function开始。在定义函数时,可以在函数名后面的括号中添加一些参数。当我们调用函数时,括号中传递的值称为参数。

function calculate(x, y) {
  return x * y;
}

calculate(2, 5);
Copy after login

Javascript中的纯函数是什么

如果函数的调用参数相同,则永远返回相同的结果。它不依赖于程序执行期间函数外部任何状态或数据的变化,必须只依赖于其输入参数。

顾名思义,纯函数跟我们初中数学的基本函数一样,遵循一定的映射关系,输入决定输出,一个输入只能对应一个输出。不同的输入可以有相同的输出,但是相同的输入不能有不同的输出

一个函数,如果符合以下两个特点,那么它就可以称之为 纯函数:

  • 对于相同的输入,永远得到相同的输出
  • 没有任何可观察到的副作用

Javascript中的构造函数是什么

构造函数是一种特殊的方法,用于初始化和创建 Javascript 中的对象。

JavaScript 中的构造函数和其它语言中的构造函数是不同的。 通过 new 关键字方式调用的函数都被认为是构造函数。

在构造函数内部,this 指向新创建的对象 Object。 这个新创建的对象的 prototype 被指向到构造函数的 prototype

如果被调用的函数没有显式的 return 表达式,则隐式的会返回 this 对象,也就是新创建的对象。

const Person = (name, age) => {
  this.name = name;
  this.age = age;
}

var man = new Person('Mark', 23);
console.log(man);
// { name: 'Mark', age: 23 }
Copy after login

在上面的代码中,我创建了一个Person构造函数,在下面的代码中,创建了一个名为man的新变量,并基于Person构造函数创建了一个新对象。

Javascript类是什么?

自从 ES6 引入以来,我们可以在Javascript中使用类。 类是一种函数,我们使用关键字class代替function 关键字来初始化它。

除此之外,我们还必须在类内部添加constructor()方法,该方法在每次初始化类时都会调用。

constructor()方法内部,我们添加了类的属性。 要基于现有的类创建另一个类,我们可以使用extends关键字。

在JavaScript中使用类的一个很好的例子是 React 框架,它是类的组件。

总结

在本文中,我收集了开发者经常问的 10 个Javascript问题,并给出答案,答案不是唯一,这里只是自己的一些见解,希望本文能给初学者带来一些帮助。

原文地址:https://dev.to/duomly/10-most-common-javascript-questions-answered-1083

作者:Duomly

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of 10 JavaScript interview questions frequently asked by developers (with answer analysis). 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)

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

PHP and Vue: a perfect pairing of front-end development tools PHP and Vue: a perfect pairing of front-end development tools Mar 16, 2024 pm 12:09 PM

PHP and Vue: a perfect pairing of front-end development tools. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

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

Is Django front-end or back-end? check it out! Is Django front-end or back-end? check it out! Jan 19, 2024 am 08:37 AM

Django is a web application framework written in Python that emphasizes rapid development and clean methods. Although Django is a web framework, to answer the question whether Django is a front-end or a back-end, you need to have a deep understanding of the concepts of front-end and back-end. The front end refers to the interface that users directly interact with, and the back end refers to server-side programs. They interact with data through the HTTP protocol. When the front-end and back-end are separated, the front-end and back-end programs can be developed independently to implement business logic and interactive effects respectively, and data exchange.

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

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

Exploring Go language front-end technology: a new vision for front-end development Exploring Go language front-end technology: a new vision for front-end development Mar 28, 2024 pm 01:06 PM

As a fast and efficient programming language, Go language is widely popular in the field of back-end development. However, few people associate Go language with front-end development. In fact, using Go language for front-end development can not only improve efficiency, but also bring new horizons to developers. This article will explore the possibility of using the Go language for front-end development and provide specific code examples to help readers better understand this area. In traditional front-end development, JavaScript, HTML, and CSS are often used to build user interfaces

Django: A magical framework that can handle both front-end and back-end development! Django: A magical framework that can handle both front-end and back-end development! Jan 19, 2024 am 08:52 AM

Django: A magical framework that can handle both front-end and back-end development! Django is an efficient and scalable web application framework. It is able to support multiple web development models, including MVC and MTV, and can easily develop high-quality web applications. Django not only supports back-end development, but can also quickly build front-end interfaces and achieve flexible view display through template language. Django combines front-end development and back-end development into a seamless integration, so developers don’t have to specialize in learning

See all articles