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

Published Wednesday, August 13, 2008 8:24 PM by Ken Cox [MVP]

Comments

# re: Visual Studio 2008 SP1 Adds JavaScript Formatting

Wednesday, August 13, 2008 11:09 PM by BrookeVigola

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

# re: Visual Studio 2008 SP1 Adds JavaScript Formatting

Thursday, August 14, 2008 5:12 AM by Dominic Pettifer

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.

# Visual Studio 2008 Service Pack

Thursday, August 14, 2008 6:51 AM by Khurram Aziz

Soma announced the availability of Visual Studio 2008 Service Pack 1 and .NET 3.5 Service Pack 1 . Installation

# Enabling JavaScript IntelliSense in External Libraries

Thursday, August 14, 2008 1:23 PM by WebLog of Ken Cox

To get JavaScript IntelliSense working in VS 2008 SP1, you need to tell IntelliSense the location of

# re: Visual Studio 2008 SP1 Adds JavaScript Formatting

Thursday, August 14, 2008 2:07 PM by Ken Cox [MVP]

@Dominic: See this post for enabling JavaScript IntelliSense:

weblogs.asp.net/.../enabling-javascript-intellisense-in-external-libraries.aspx

Ken

# re: Visual Studio 2008 SP1 Adds JavaScript Formatting

Monday, August 18, 2008 10:46 PM by Edward Ng

The VS2008 SP1 JScript formatter sucks !

# re: Visual Studio 2008 SP1 Adds JavaScript Formatting

Tuesday, October 07, 2008 3:16 PM by DarrenFieldhouse

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.

# re: Visual Studio 2008 SP1 Adds JavaScript Formatting

Sunday, November 08, 2009 2:49 AM by Melwin

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

# re: Visual Studio 2008 SP1 Adds JavaScript Formatting

Tuesday, May 18, 2010 12:06 PM by avejidah

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

# re: Visual Studio 2008 SP1 Adds JavaScript Formatting

Monday, August 23, 2010 10:47 AM by cherbert

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.

Leave a Comment

(required) 
(required) 
(optional)
(required)