Contents

[edit]

JSLint

jQuery core passes JSHint (not JSLint) with a few stipulations. To run JSHint against jQuery you can download jQuery from Git and run 'make hint'.

We ignore the following warnings from JSHint:

Additionally we enable the following two JSLint options by default:

[edit]

Spacing

Use tabs to indent your code.

Be sure to use liberal spacing in your code.

   // No:
   if(blah==="foo"){
     foo("bar","baz",{zoo:1});
   }
   
   // Yes:
   if ( blah === "foo" ) {
     foo( "bar", "baz", { 
       zoo: 1 
     });
   }

Opening braces are always prefixed by a space.

Don't use tabs inside a line.

   var a = true,
     // yayy
     c = false,
     // grrr
     b   = false;

Lines with nothing on them should have no whitespace.

There should be no whitespace at the end of a line.

[edit]

Comments

Long comments should use /* ... */.

Single line comments should always be on their own line and be above the line they reference. Additionally there should be an extra endline above it. For example:

   var some = "stuff";
   
   // We're going to loop here
   for ( var i = 0; i < 10; i++ ) {}


Inline comments are allowed as an exception when used to annotate special arguments in formal parameter lists:

   function foo( types, selector, data, fn, /*INTERNAL*/ one ) { 
      // do stuff.
    }
[edit]

Equality

Strict equality checks (===) should be used in favor of == wherever possible.

[edit]

Blocks

if/else/for/while/try always have braces and always go on multiple lines.

Braces should always be used on blocks.

   // NO:
   if ( true )
      blah();
   
   // YES:
   if ( true ) {
      blah();
   }

Don't put statements on the same line as a conditional.

   // NO:
   if ( true ) return;
   if ( true ) blah();
   
   // YES:
   if ( true ) {
      return;
   }
   
   if ( true ) {
      blah();
   }

else/else if/catch go on the same line as the brace.

   if ( blah ) {
      baz();
   } else {
      baz2();
   }

Exception: else if is allowed, a else { } is not required

   } else if ( test ) {
     blah();
   }
   
   // Not needed:
   } else {
     if ( test ) {
       blah();
     }
   }

Don't use ternary operators instead of if/else statements.

Don't use object && object.method() instead of if/else statements (except in conditionals).

[edit]

Function Calls

Always include extra spaces around the arguments.

   foo( true );
   

Exception: foo(true) (no spaces) is allowed if you're already inside of another function call.

   foo( bar(true) );

Exception: Functions, object literals, array literals and string literals go snug to front and back of the parentheses - but ONLY when it's the only argument.

   foo(function() { });
   foo({   });
   foo([   ]);
   foo(" ");

Exception: Multi-line function/object/array literals go snug at end (and at start ONLY if they are the only argument).

   WRONG: 
   foo( true, { blah: "baz" });
   
   RIGHT: 
   foo( true, {
     blah: "baz"
   });

Empty functions, object literals and array literals don't need filler spaces.

   foo();
   {}
   []


Always have spaces after commas and colons.

[edit]

Arrays and Objects

Empty objects and arrays don't need filler spaces.

 []
 {}

Always have spaces after commas and colons.

[edit]

Assignment

Assignments should always have a semicolon after them.

Semicolons should always be followed by an endline.

Assignments in a declaration should always be on their own line. Declarations that don't have an assignment should be listed together at the start of the declaration. For example:

   var a, b c,
     test = true,
     test2 = false;
[edit]

Type Checks

[edit]

RegExp

All RegExp operations should be done using .test() and .exec(). "string".match() is no longer used.

[edit]

Strings

Strings should always use double-quotes instead of single-quotes.

[edit]

Nodes

.nodeName should always be used in favor of .tagName.

.nodeType should be used to determine the classification of a node (not .nodeName).

Don't attach expandos to nodes - only attach data using .data().