Questions you should pay attention to when programming JavaScript
JavaScript is a programming language that is the most popular in the world and can be used for web development, mobile application development (PhoneGap, Appcelerator), server-side development (Node.js and Wakanda), etc. JavaScript is also the first language for many novices to enter the world of programming. It can be used to display a simple prompt box in the browser, or to control the robot through nodebot or nodruino. Developers who can write JavaScript code with clear structure and high performance are now the most sought after people in the recruitment market.
In this article, I will share some JavaScript tips, secrets, and best practices that, with a few exceptions, apply to both the browser's JavaScript engine and the server-side JavaScript interpreter.
The sample code in this article passed the test on the latest version of Google Chrome 30 (V8 3.20.17.15).
1. Be sure to use the var keyword when assigning a value to a variable for the first time.
If a variable is assigned directly without a declaration, it will be used as a new global variable by default. Try to avoid using global variables.
2. Use === instead of ==
== and the != operator will automatically convert the data type when necessary. But === and !== don't, they compare values and data types at the same time, which also makes them faster than == and !=.
[10] === 10 // is false[10] == 10 // is true'10' == 10 // is true'10' === 10 // is false[] == 0 // is true[] === 0 // is false'' == false // is true but true == "a" is false'' === false // is false
3. The logical results of underfined, null, 0, false, NaN, and empty strings are all false
4. Use a semicolon at the end of the line
In practice, it is best to use It’s okay if you forget to write a semicolon. In most cases, the JavaScript interpreter will add it automatically. For more information on why semicolons are used, please refer to the article The Truth About Semicolons in JavaScript.
5. Use the object constructor
function Person(firstName, lastName){ this.firstName = firstName; this.lastName = lastName;
}var Saad = new Person("Saad", "Mousliki");
① Niuke.com input stream: var line=readline().split(' ');
② Saima.com input stream: var line=read_line().split(' ');
③ Output stream: print();
④ Note: Do not perform DOM related operations when answering on Saima.com. Read a line of input: read_line(), output a line: print(something); when using the read_line() function, please pay attention If a line of input exceeds 1024 characters, you need to call read_line multiple times to read the input and then splice it yourself; when using the print function to output, please note that a newline character is automatically included at the end, and there is no need to add it yourself.
1 //注意,如果一行超过1024个字符,会被强制分行的 2 //,因此如果题目明确说明该行超过1024字符,请自行拼接(当然,我们尽量不出这种题目)。 3 /* 4 var next = ''; 5 var line; 6 while(line = read_line()){ 7 next += line; 8 } 9 next中就是超过1024字符的该行字符串。10 */
⑤ The following is the sample code for the A+B question
1 var line;2 while(line=readline()){3 var lines = line.split(' ');4 var a = parseInt(lines[0]);5 var b = parseInt(lines[1]);6 print(a+b);7 }
<br/>
The above is the detailed content of Questions you should pay attention to when programming JavaScript. 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

AI Hentai Generator
Generate AI Hentai for free.

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



Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

During the Mingchao test, please avoid system upgrades, factory resets, and parts replacement to prevent information loss and abnormal game login. Special reminder: There is no appeal channel during the testing period, so please handle it with caution. Introduction to matters needing attention during the Mingchao test: Do not upgrade the system, restore factory settings, replace equipment components, etc. Notes: 1. Please upgrade the system carefully during the test period to avoid information loss. 2. If the system is updated, it may cause the problem of being unable to log in to the game. 3. At this stage, the appeal channel has not yet been opened. Players are advised to choose whether to upgrade at their own discretion. 4. At the same time, one game account can only be used with one Android device and one PC. 5. It is recommended that you wait until the test is completed before upgrading the mobile phone system or restoring factory settings or replacing the device.

With the rise of short video platforms, Douyin has become an indispensable part of many people's daily lives. Live broadcasting on Douyin and interacting with fans are the dreams of many users. So, how do you start a live broadcast on Douyin for the first time? 1. How to start a live broadcast on Douyin for the first time? 1. Preparation To start live broadcast, you first need to ensure that your Douyin account has completed real-name authentication. You can find the real-name authentication tutorial in "Me" -> "Settings" -> "Account and Security" in the Douyin APP. After completing the real-name authentication, you can meet the live broadcast conditions and start live broadcast on the Douyin platform. 2. Apply for live broadcast permission. After meeting the live broadcast conditions, you need to apply for live broadcast permission. Open Douyin APP, click "Me"->"Creator Center"->"Direct

Steps and precautions for using localStorage to store data This article mainly introduces how to use localStorage to store data and provides relevant code examples. LocalStorage is a way of storing data in the browser that keeps the data local to the user's computer without going through a server. The following are the steps and things to pay attention to when using localStorage to store data. Step 1: Check whether the browser supports LocalStorage

Methods and precautions for installing pip in an offline environment. Installing pip becomes a challenge in an offline environment where the network is not smooth. In this article, we will introduce several methods of installing pip in an offline environment and provide specific code examples. Method 1: Use the offline installation package. In an environment that can connect to the Internet, use the following command to download the pip installation package from the official source: pipdownloadpip This command will automatically download pip and its dependent packages from the official source and save it in the current directory. Move the downloaded compressed package to a remote location

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

Notes and FAQs on MyBatis batch query statements Introduction MyBatis is an excellent persistence layer framework that supports flexible and efficient database operations. Among them, batch query is a common requirement. By querying multiple pieces of data at one time, the overhead of database connection and SQL execution can be reduced, and the performance of the system can be improved. This article will introduce some precautions and common problems with MyBatis batch query statements, and provide specific code examples. Hope this can provide some help to developers. Things to note when using M

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
