Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
Last active September 26, 2023 07:45
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save sindresorhus/4267148 to your computer and use it in GitHub Desktop.
Save sindresorhus/4267148 to your computer and use it in GitHub Desktop.
My preferred code style.

Code Style

  • Tab indentation
  • Single-quotes
  • Semicolon
  • Strict mode
  • No trailing whitespace
  • Multiple variable statements
  • Space after keywords and between arguments and operators
  • Return early
  • JSHint valid
  • Consistency

Example:

'use strict';

function foo(bar, fum) {
    var ret;
    var hello = 'Hello';

    if (!bar) {
        return;
    }

    for (var i = 0; i < bar.length; i++) {
        if (bar[i] === hello) {
            ret += fum(bar[i]);
        }
    }

    return ret;
}

Read idiomatic.js for general JavaScript code style best practices.

@sindresorhus
Copy link
Author

See XO for this code style as a linter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment