Tools for JS Style Guidelines

Question
What are some useful tools for enforcing JavaScript style guidelines?

Answer
Two are JSLint and JSHint.

Comments
JSLint is a tool written by famed Douglas Crockford. It’s a tool that detects potential errors and stylistic concerns in your code. JSHint is a fork of JSLint, and it provides a less strict, more customizable experience. Both are excellent options, and integrating one or both into your development process is a great way to enforce JS code conventions. Additionally, Crockford has provided rules on JavaScript style in the following documents:
Part One deals with syntax and basic patterns.
Part Two deals with language idioms.
Code Conventions for the JavaScript Language contains points from the first two, with a small number of style guidelines.

Validating Bit-Coded Enum Values

Question
What’s the recommended way to check bit-coded enum values?

Answer
The following code demonstrates how to test bit-coded enum values.

FileProperty fileProperty = GetFileProperties();

// Incorrect. This will fail if fileProperty has more than one bit set.
if ( fileProperty == FileProperty.Hidden ) {
    ...
}

// Also incorrect. This cannot test multiple bits at once.
if ( (fileProperty & FileProperty.Hidden ) != 0 ) {
    ...
}

// The correct way.
if ( (fileProperty & FileProperty.Hidden) == FileProperty.Hidden ) {
    ...
}

// An example that checks whether a file is System AND Hidden.
FileProperty property = FileProperty.System | FileProperty.Hidden;
if ( (fileProperty & property) == property ) {
    ...
}

Validating Enum Values

Question
Should non-bit-coded enum values be validated when passed to properties or methods as arguments?

Answer
Yes, always validate these values.

Comments
Why? Using an enum parameter only guarantees the value being passed in is an integer. It doesn’t guarantee the value is one of the constants defined in the enum, and it’s possible to define invalid enum values. For example:

DayOfTheWeek dayOfWeek = (DayOfTheWeek) 100;

Example
Here’s an example of how to properly implement a property that takes and returns enum members with no invalid values.

private DayOfTheWeek weekDay = DayOfTheWeek.Sunday;

public DayOfTheWeek WeekDay {
    get {
        return weekDay;
    }
    set {
        if ( value < DayOfTheWeek. Sunday || value > DayOfTheWeek.Saturday ) {
            throw new ArgumentException("Invalid day of the week.");
        weekDay = value;
        }
    }
}