28

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?

11 Answers 11

23

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.

0
17

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.
2
  • 2
    This tool really pin points the error in javascript. Thanks for the link. It saved lot of time and frustration. Commented Oct 5, 2011 at 23:07
  • The server at www.javascriptlint.com can't be found, because the DNS lookup failed. Commented Nov 17, 2015 at 16:59
8

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

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

1
  • 1
    This won't look for syntax errors, it will look for code smells.
    – Flimm
    Commented Aug 23, 2018 at 14:48
7

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.

3

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

1
3

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).

1
  • You can install with npm install --global esprima and then run the javascript syntax validator on a file with esvalidate path/to/file.js.
    – mareoraft
    Commented Feb 27, 2019 at 21:48
2

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.

2

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

1

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.

1

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

0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.