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!