Analysis of the latest JS interview questions
For a web front-end, you will definitely encounter some JS interview questions during the interview. Today I will summarize some of the latest JS interview questions for you. Each question is impressive
Pre-parsing: In the current scope, before js is run, the var and function keywords will be declared in advance, but will not be assigned (personal opinion)
I am impressed by the pre-parsing, not because it is difficult , but be careful. If you are a little careless, you may write the wrong answer! I encountered more than one pre-analysis question, and I can still remember two of them. Let me tell you!
2-1. Pre-analysis 1
alert(a) a(); var a=3;function a(){ alert(10) } alert(a) a=6; a();
------------Separating line---------------- --
alert(a) a(); var a=3; var a=function(){ alert(10) } alert(a) a=6; a();
When I saw this code, I answered wrongly. Later, I asked a friend for advice, and then I understood it myself, and it was straightened out!
There are actually two test points. The first is that the variable is declared in advance, and the second is that the function is declared before the variable is declared!
Let me briefly analyze it below,
The first part of the operation results:
1. Function declaration takes precedence over variable declaration, so at the beginning, a is function a(){alert(10)}, you will see to this function.
2.a(), when the function is executed, alert(10) appears
3. Var a=3 is executed; so alert(a) displays 3
4. Since a is not a function, So when executing a(), an error will be reported.
The second part of the operation results:
1.underfind
2. Error report
As mentioned before, pre-parsing is a pre-declaration with the var and function keywords, but no value assignment. So at first it was underfind, and then an error was reported because when a() was executed, a was not a function.
//Function expression is the same as variable declaration
var a=function(){ alert(10) }
//Function declaration is better than variable declaration
function a(){ alert(10) }
2-2. Pre-parsing and scope
var a=0;function aa(){ alert(a) a=3 }
//The result is that nothing happens, because alert(0) is executed only when the aa function is executed
------ ------Separating line 1------------------
var a=0;function aa(){ alert(a) var a=3 } aa();
//underfind In the aa function, there is var a=3, then in the aa scope, the variable a is declared in advance, but it will not be assigned a value, so it is underfind
------------Separating line 2-- ----------------
var a=0;function aa(a){ alert(a) var a=3 } aa(5) alert(a)
//5,0 In the function body, the priority of parameter a is higher than that of variable a
------------Separating line 3------------------
var a=0;function aa(a){
alert(a)
a=3
}
aa(5)
alert(a)
//5,0 In the function body, execute alert (a) and a=3, what is modified is not the global variable a, but the parameter a
------------Separating line 4--------- ---------
var a=0;function aa(a){ alert(a) var a=3 alert(a) } aa(5)
//5,3
//I also don’t understand this a little bit. Please ask the explanation on the Internet. There are two answers (small If you know how to understand, please give us some advice in the comments)
//1. Parameter priority is higher than variable declaration, so the declaration of variable a is actually ignored, which is equivalent to
//var a=0 ;
//function aa(a){
// var a=5;
// alert(a)
// a=3
// alert(a)
//}
//aa(5)
//2. Formal parameters and local variables have the same priority, which is equivalent to
//var a=0;
/ /function aa(a){
// var a; First declare
// a=5 Since the formal parameters and variable names are the same, they are covered!
// alert(a)
// a=3
// alert(a)
//}
//aa(5)
--- ----------Separation line 5------------------
var a=0;function aa(a){ alert(a) a=3 alert(a) } aa() alert(a) //underfind 3 0
//First, parameter priority Higher than the global variable. Since no parameters are passed, it is underfind
//a=3. In fact, the value of the formal parameter a when modified is not the global variable a. Going down alert(a) is also the formal parameter a##. #//The last alert(a), you know it
var arr=[];for(var i=0;i<10;i++ ){ i<=1?arr.push(1):arr.push(arr[i-1]+arr[i-2]); } console.log(arr)
How to do this? At that time, my approach was written in two steps, showing the front first, and then showing the back.
The code is
//01234543210 //先展示前面的 01234 //n:开始的数字 m:结束的数字function num1(n,m){ for(var i=n;i<m;i++){ //再展示后面的 543210 console.log(i); if(i===m-1){ num2(n,m) } } }function num2(n,m){ for(var i=m;i>=n;i--){ console.log(i) } } num1(2,5) //2345432
function num(n,m){ console.log(n); if(n<m){ num(n+1,m); console.log(n); } } num(2,5) //2345432
console.log(2); -> num(3,5); -> console.log(2); //执行num(3,5); 就是是相当于 console.log(3); -> num(4,5); -> console.log(3); 下面以此类推 console.log(2); -> console.log(3); -> num(4,5); -> console.log(3); -> console.log(2);
console.log(2); -> console.log(3); -> console.log(4); -> num(5,5); -> console.log(4); -> console.log(3); -> console.log(2);
console.log(2); -> console.log(3); -> console.log(4); -> console.log(5); -> console.log(4); -> console.log(3); -> console.log(2);
How to create a butterfly flying animation with CSS3
How to use canvas to achieve it The interaction between the ball and the mouse
How to use canvas to create the effect of particle fountain animation
The above is the detailed content of Analysis of the latest JS interview questions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In-depth analysis of the role and application scenarios of HTTP status code 460 HTTP status code is a very important part of web development and is used to indicate the communication status between the client and the server. Among them, HTTP status code 460 is a relatively special status code. This article will deeply analyze its role and application scenarios. Definition of HTTP status code 460 The specific definition of HTTP status code 460 is "ClientClosedRequest", which means that the client closes the request. This status code is mainly used to indicate

iBatis and MyBatis: Differences and Advantages Analysis Introduction: In Java development, persistence is a common requirement, and iBatis and MyBatis are two widely used persistence frameworks. While they have many similarities, there are also some key differences and advantages. This article will provide readers with a more comprehensive understanding through a detailed analysis of the features, usage, and sample code of these two frameworks. 1. iBatis features: iBatis is an older persistence framework that uses SQL mapping files.

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

Wormhole is a leader in blockchain interoperability, focused on creating resilient, future-proof decentralized systems that prioritize ownership, control, and permissionless innovation. The foundation of this vision is a commitment to technical expertise, ethical principles, and community alignment to redefine the interoperability landscape with simplicity, clarity, and a broad suite of multi-chain solutions. With the rise of zero-knowledge proofs, scaling solutions, and feature-rich token standards, blockchains are becoming more powerful and interoperability is becoming increasingly important. In this innovative application environment, novel governance systems and practical capabilities bring unprecedented opportunities to assets across the network. Protocol builders are now grappling with how to operate in this emerging multi-chain

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

Analysis of new features of Win11: How to skip logging in to a Microsoft account. With the release of Windows 11, many users have found that it brings more convenience and new features. However, some users may not like having their system tied to a Microsoft account and wish to skip this step. This article will introduce some methods to help users skip logging in to a Microsoft account in Windows 11 and achieve a more private and autonomous experience. First, let’s understand why some users are reluctant to log in to their Microsoft account. On the one hand, some users worry that they

Detailed analysis and examples of exponential functions in C language Introduction: The exponential function is a common mathematical function, and there are corresponding exponential function library functions that can be used in C language. This article will analyze in detail the use of exponential functions in C language, including function prototypes, parameters, return values, etc.; and give specific code examples so that readers can better understand and use exponential functions. Text: The exponential function library function math.h in C language contains many functions related to exponentials, the most commonly used of which is the exp function. The prototype of exp function is as follows

Due to space limitations, the following is a brief article: Apache2 is a commonly used web server software, and PHP is a widely used server-side scripting language. In the process of building a website, sometimes you encounter the problem that Apache2 cannot correctly parse the PHP file, causing the PHP code to fail to execute. This problem is usually caused by Apache2 not configuring the PHP module correctly, or the PHP module being incompatible with the version of Apache2. There are generally two ways to solve this problem, one is
