开发者

How to find Javascript syntax errors? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
开发者_C百科

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 6 years ago.

Improve this question

I am writing a JavaScript section of code and making huge amounts of silly mistakes in the syntax. But the only way to find what line the error is on is to start commenting out the section of code I just wrote and reload it into the browser to narrow down where my missing ');' is.

How do you "compile" a JavaScript source to make sure it is syntactically correct so that I can debug it in the browser?


Ctrl + Shift + J in firefox, if you don't catch your mistakes until runtime. Of course firebug works better, but this is a quick way.


http://www.javascriptlint.com/

Here are some common mistakes that JavaScript Lint looks for:

  • Missing semicolons at the end of a line.
  • Curly braces without an if, for, while, etc.
  • Code that is never run because of a return, throw, continue, or break.
  • Case statements in a switch that do not have a break statement.
  • Leading and trailing decimal points on a number.
  • A leading zero that turns a number into octal (base 8).
  • Comments within comments.
  • Ambiguity whether two adjacent lines are part of the same statement.
  • Statements that don't do anything.


Douglas Crockford's Jslint: http://www.jslint.com/

But be warned, it will hurt your feelings ;-)


Chrome : Ctrl + Shift + J : this will bring up the JavaScript console. You can click the scripts tab to review page scripts.

FireFox : Install firebug and run it to get a similar console to the above stated chrome utilities.

IDE : You may wish to use netbeans or Eclipse which both offer syntax highlighting and code completion for JavaScript. These will indicate syntax errors at code time.

jslint/jshint : These tools offer code suggestions to improve your code quality and reduce the possibility of errors (logic) but will also break on syntax and other errors.


Try JSlint

It's a great utility for debugging Javascript. If you're using the jQuery library, there is a plugin being developed and was just recently released, similarly called jQuery Lint


You can use the Esprima JavaSript parser (the link is a online code editor ready to use).

I believe it's currently the best ready-to-use js parser in js, that's why I'm using it in my LIVEditor project (code editor for the html/css/js with real-time preview).


One idea would be to use Firebug console for interactively experimenting with you scripts and then moving only the tested parts into the your code.


I find JSHint, + its vim plugin are very useful. Light weight of vim and still be able to track the syntax errors of the javascript.

https://github.com/walm/jshint.vim


The Eclipse IDE (with the JSEclipse plugin) identifies nearly all of our syntax problems.

I suggest one of the better editors is IntelliJ, but it's not free.


If you are using Textmate (mac) you can install this bundle

It is mainly jslint, that will check your JS syntax.
And it contains some other goodies like various compression tools


Javascript is run by the browser. I'm not sure there are are 'compilers' so to speak.

One thing you could try is Firefox with Firebug addon installed. You can easily refresh the page, get error listing, including line numbers, etc. Easy way to debug.

Also - If you are willing, using Visual Studio 2008 (or the free web developer version) gives you Javascript intellisense, including jQuery support. This will help you immensely in keeping your code clean as you go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜