179

What are the established coding standards for JavaScript?

8
  • 1
    Seeing this question still shows up in google search I thought adding this might be relevant. JavaScript is an implementation of ECMAScript script, following the ECMA standards. Standard ECMA-262. To add from the ECMA wiki page: Netscape delivered JavaScript to Ecma International for standardization and the work on the specification, ECMA-262, began in November 1996
    – Nope
    Oct 22, 2013 at 9:11
  • 13
    Closed AND protected. Huh!?!
    – Robbie Dee
    Oct 29, 2013 at 12:49
  • 1
  • 13
    // , WHHHHYYYY was this closed? It's an excellent question for new Javascript programmers. Especially given the, ah, state of most original Javascript code written by new programmers. Jun 11, 2015 at 5:10
  • 1
    This is VERY constructive, and an excellent fit for the SE Q&A format. Jul 12, 2016 at 4:09

8 Answers 8

75

Not exactly what you're looking for, but (like Doug Crockford's vids) I think everybody who writes/edits/reviews JS should look at Chris Heilmann's slides and videos on maintainable javascript.

In the slides, slide 127, he says don't worry so much about style guides/code standards, reformat all code checked in to VCS.

Also these:

Mozilla JavaScript Tips

JavaScript Style Guide

4
  • 2
    Google also has a style guide: google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
    – aug
    Oct 11, 2013 at 9:03
  • 3
    In an attempt to solve this question (finding and deciding on a JavaScript Code Style, while allowing for some flexibility), I've created jscode.org where you can generate custom code styles according to some settings (yeah, it may sound like blatant advertising, but I'm not gaining anything there, I just built this for myself and my team, and I think it's of good value). Oct 27, 2013 at 15:30
  • The Mozilla JavaScript Tips is very sparse, I wouldn't include it. Jul 12, 2016 at 4:12
  • All the links broken. this is an example of a BAD Answer
    – ecarrizo
    Mar 1, 2022 at 10:02
24

Another reason why Crockford's JavaScript coding guidelines are a good idea to follow:

This code (below) actually returns undefined because of JavaScript's semicolon injection - which is a damn good reason to get the whole team of developers singing from the same song sheet:

return // injected semicolon, therefore returns 'undefined'
{
    javascript : "fantastic"
}; // object constructs anonymously but nothing happens with it.

Bit more about that at Beware of JavaScript semicolon insertion

1
  • 2
    There is also JSLint to help automate/verify some of these guidelines. jslint.com
    – Funka
    Aug 20, 2009 at 4:06
10

I never saw a widely acknowledged JavaScript coding standard out there. We did write our own some time ago and I published it on my blog in accordance with my employer.

Keep in mind that some of the guidelines might be a bit outdated.

1
  • This document has reference to 'C# programming guidelines'. Do you have it publicly available as well?
    – z-boss
    Oct 17, 2008 at 14:17
8

Google has posted a Javascript guide

1
  • This is a "link only" answer and the link does not work anymore which is exactly why "link only answers" should be avoided.
    – MrUpsidown
    Sep 13, 2016 at 15:03
6

You can find some simple but essential coding conventions at the Google Code Wiki.

For very detailed style guides read the Dojo Style Guide. It is going over naming conventions, file conventions, variable usage, code layout, whitespace, and comments.

4

A bit puzzled that I cannot find on the replies Idiomatic.js and jQuery Core Style Guide. Addy Osmani puts those two on the very top of full list of coding styles for JavaScript in his "JavaScript Style Guides And Beautifiers" article.

I personally prefer jQuery standard, though it doesn't much differ from Idiomatic.js. But whatever standard you choose you will need a validator to check how successful you are following it. I couldn't find any for jQuery or at least Idiomatic, but eventually came up with JS_CodeSniffer

3

The comp.lang.javascript Code Guidelines document points to causes of errors and problems related to javascript for web applications and how to avoid them.

2

Myself and a few others recently put together these (PDF or HTML)

They are primarily intended for use with the Ajax Control Toolkit but realistically contain all sorts of useful guidance whichever framework you use.

Hope that helps.

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