24 Useful tips for novice JavaScript

Source: Internet
Author: User
Tags closing tag script tag

This article lists 24 suggestions that will make your code writing process easier and more efficient. Perhaps you are a JavaScript beginner, just finished your own Hello world, there are a lot of tips for your work will be very useful, perhaps some skills you already know, then try a quick glance, see if you can find something new! 1. Use = = = Instead of ==javascript There are two different equality operators: ===|! = = and ==|! = In contrast, the former is more recommendable. Please use the former as much as possible. "If two comparison objects have the same type and value, = = = Returns true,!== returns FALSE." "–javascript:the Good parts However, if you use = = and! =, you may experience some unexpected problems when you manipulate different data types. JavaScript attempts to convert them to string, number, or Boolean amounts before making an equal judgment. 2. Avoid using the Eval function The Eval function takes a string as an argument and executes the string as a JavaScript statement, returning the result (reference). This function will not only reduce the efficiency of your script execution, but also greatly increase the security risk, because it gives the argument as text too much power. Please don't use it! 3. Do not use quick notation technically, you can omit most of the curly brackets and end-of-line semicolons, and most browsers can execute the following statements correctly:. if (somevariableexists) x = False However, if so:. if (    somevariableexists) x = False Anotherfunctioncall (); You might think it is equal to the following statement: if (somevariableexists) {x = false; Anotherfunctioncall (); Unfortunately, that's not the case. The reality is that it is equivalent to: if (somevariableexists) {x = false;} anotherfunctioncall (); If you notice, the beautiful indentation is not a substitute for this gorgeous curly bracket. In all cases, write a clear curly brace and a semicolon. It can be omitted occasionally when there is only one line of statements, although doing so is extremely deprecated: if (2 + 2 = = 4) return ' nicely done '; Think about the future, kid. Suppose you need to add more commands to this if statement in the future development process? Then you're not going to have to add the brackets? 4. Make good use of JS Lintjslint is by Douglas CrA debugger written by Ockford. You just need to post your code and it will quickly scan you for any obvious errors and problems. The JSLint scans the received code. Identify the problem, describe the problem, and give its approximate location in the source code. Problems that can be found include but are not limited to grammatical errors, although grammatical errors are indeed the most common. JSLint also uses customary habits to check the formatting style of the code, as well as the structural errors. Scanning through the JSLint does not guarantee that your program is completely correct. It just gives you an extra pair of eyes to find the wrong one. "–jslint the document before you complete the code, put it in the jslint to check it and quickly eliminate your unintentional." 5. Load the script at the bottom of the page as shown: Remember-we do everything we can to ensure that the client page loads as fast as possible. When the script is not loaded, the browser cannot load the rest of the page. If your JS file just adds some extra features--for example, to click on a link binding event--that big can wait until the page load is basically done. This is best done by placing the JS file at the end of the page, before the body's closing tag. Better writing is:<p> is the most handsome person in the world. Benhuoer.com is the best-looking blogger in the world. </p> <script type= "Text/javascript" src= "path/to/file.js" ></script> <script type= "text/ JavaScript "src=" Path/to/anotherfile.js "></script> </body> 

24 Useful tips for novice JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.