Visual Studio 2008 SP1 Adds JavaScript Formatting

Just a "two thumbs up" for the addition of JavaScript™/JScript® formatting in Visual Studio 2008 Service Pack 1. I don't like leaving messy-looking code - especially since JavaScript makes the trip all the way to the client.

My formatting preference is to show opening and closing braces on a new line, so here's what you need to do in VS 2008:

  1. From the Tools menu, click Options.
  2. In the Options dialogue box, check Show All Settings in the bottom left corner. (BTW, it makes no sense for Microsoft to hide so many excellent tools.)
  3. Expand the Text Editor node, expand JScript and select Formatting.
  4. In the New Lines group box, check both Place Open Brace On New Line For Functions and Place Open Brace On New Line For Control Blocks.
  5. Click OK to close the Options dialogue box.
  6. In a .js page, press Ctrl+K, Ctrl+D (or, From the Edit menu, click Format Document).

This is one of those productivity improvements that makes the IDE indispensable.

Ken

8 Comments

  • I'd rather put it in a library and compact it. I'd rather not download your indentation and line returns Thank You.

  • Weren't they going to add Javascript Intellisence for various Javascript frameworks like JQuery? If they have, how do I enable it? I can't seem to get it work.

  • @Dominic: See this post for enabling JavaScript IntelliSense:

    http://weblogs.asp.net/kencox/archive/2008/08/14/enabling-javascript-intellisense-in-external-libraries.aspx

    Ken

  • The VS2008 SP1 JScript formatter sucks !

  • Despite the negative comments I think the formatting is great, although the default of open braces on the same line was annoying me until I found this post! Thanks.

  • Javascript formatting was driving me crazy, until I did some search and found this post. Thanks a lot!

  • Thanks man, I hated how it put braces on the same line.

  • Actually, in js it's always a good idea to put opening braces on the same line due to semi colon insertion. Consider the following example:

    return {
    name : 'John',
    age : 22
    };

    if you use new line bracing that becomes:

    return
    {
    name : 'John',
    age : 22
    };

    The browser will see the empty return statement and insert a semicolon. The empty object declaration is allowed in js so, instead of getting your person back, you'll actually get no errors and a null object.

Comments have been disabled for this content.