A Little JavaScript Formatting

So I am a stickler when it comes to code formatting. If I see this:

view plain print about
1<script type="text/javascript">
2    function demo(){
3        var hello = "world";
4        var someNumber = 132;
5        var anotherString = "some string value";
6 }
7</script>

I cringe and tell you to clean up your code. Instead of doing that you can do this:

view plain print about
1<script type="text/javascript">
2    function demo(){
3        var hello = "world",
4            someNumber = 132,
5            anotherString = "some string value";
6    }
7</script>

Also, in development putting them on new lines is great for readability. Coldfusion Builder by default wants to format them to one line.

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Aaron J's Gravatar I was right there with you because JSLint would recommend it. But lately since its a best practice for performance to minify and concat your JS files it doesn't really matter.

In fact, I've decided that I'd rather make my code as readable as possible and not worry about white space, etc, and now prefer to use multiple vars. The keyword usually lights up in IDEs making it easy to see your var scoping.

Then when you publish to prod just run your build tool (like in RequireJS or HTML5BP, etc) and it automatically removes your extra vars and reduces white space to make it as tiny as possible. Win.
# Posted By Aaron J | 3/13/12 6:01 PM
Ryan VIkander's Gravatar The new line is for development only, of course going to production you would want to minify your JS files.
# Posted By Ryan VIkander | 3/13/12 6:05 PM
Aaron Johnson's Gravatar I saw this recently and it reminded me of your blog post. Its very relevant to what I was saying.

http://benalman.com/news/2012/05/multiple-var-stat...
# Posted By Aaron Johnson | 5/8/12 12:35 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.6.004. Contact Blog Owner