Ajax Basics Tutorial (5)-5.4 Using JSLint to complete JavaScript syntax checking

Source: Internet
Author: User

JSLint is a JavaScript verification tool (Www.jslint.com) that scans JavaScript source code to find problems. If JSLint finds a problem, JSLint displays a message that describes the problem and points out the approximate location of the error in the source code. Some coding style conventions may lead to unforeseen behavior or errors, and JSLint can mark out structural problems in addition to the unreasonable conventions. Although JSLint does not guarantee that the logic must be correct, it does help to discover errors that are likely to cause the browser's JavaScript engine to throw errors.

JSLint defines a set of coding conventions that are more restrictive than the language defined by ECMA. These coding conventions draw on the rich coding experience of many years and use an age-old programming principle as a tenet: being able to do doesn't mean you should. JSLint will flag code practices that it considers risky, and it will also point to obvious errors (see figure 5-12), prompting you to develop good JavaScript coding habits.

Figure 5-12 JSLint will check for errors and bad coding styles to provide JavaScript validation

JSLint may have identified some structural errors as questionable coding practices, some of which are listed below (the complete list can refer to JSLint's documentation).

JSLint requires that all lines of code end with semicolons. Although JavaScript does allow newline characters to be used as a line terminator, it is generally considered ambiguous and a bad coding style.

Statements using if and for are required to enclose the block of statements using curly braces.

Unlike other programming languages, in JavaScript, a block is not scoped to a variable. JavaScript only supports function-level scopes. Therefore, JSLint only accepts blocks that are part of the function, if, switch, while, for, do, and try statements, and all other blocks are flagged as errors.

var can only be declared once, and must be declared before it is used.

JSLint the code that appears after the return, break, continue, or throw statement as an unreachable code. These statements must be followed by an end brace.

JSLint is a great tool for JavaScript programmers as a novice, because it teaches you some good JavaScript coding practices. Because JSLint can label parts that may cause logical errors or other unforeseen behavior, you can reduce debugging time. If you have trouble debugging a piece of JavaScript code, try JSLint.

Back to "Ajax Basics Tutorial-Directory"

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.