A mostly reasonable approach to JavaScript
- Types
- Objects
- Arrays
- Strings
- Functions
- Properties
- Variables
- Hoisting
- Conditional Expressions & Equality
- Blocks
- Comments
- Whitespace
- Leading Commas
- Semicolons
- Type Coercion
- Naming Conventions
- Accessors
- Constructors
- Modules
- jQuery
- ES5 Compatability
- Testing
- Performance
- Resources
- The JavaScript Style Guide Guide
- Contributors
- License
-
Primitives: When you access a primitive type you work directly on its value
StringNumberBooleannullundefined
var foo = 1, bar = foo; bar = 9; console.log(foo, bar); // => 1, 9
-
Complex: When you access a complex type you work on a reference to its value
ObjectArrayFunction
var foo = [1, 2], bar = foo; bar[0] = 9; console.log(foo[0], bar[0]); // => 9, 9
-
Use the literal syntax for object creation.
// bad var item = new Object(); // good var item = {};
-
Don't use reserved words as keys.
// bad var superman = { class: 'superhero', default: { clark: kent }, private: true }; // good var superman = { klass: 'superhero', defaults: { clark: kent }, hidden: true };
-
Use the literal syntax for array creation
// bad var items = new Array(); // good var items = [];
-
For performance reasons use direct assignment over Array#push
var len = items.length, itemsCopy = [], i; // bad for (i = 0; i < len; i++) { itemsCopy.push(items[i]) } // good for (i = 0; i < len; i++) { itemsCopy[i] = items[i]; }
-
Use single quotes
''for strings// bad var name = "Bob Parr"; // good var name = 'Bob Parr'; // bad var fullName = "Bob" + this.lastName; // good var fullName = 'Bob' + this.lastName;
-
Strings longer than 80 characters should be written across multiple lines using string concatenation.
// bad var errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.'; // bad var errorMessage = 'This is a super long error that \ was thrown because of Batman. \
When you stop to think about \ how Batman had anything to do \ with this, you would get nowhere \ fast.'; // good var errorMessage = 'This is a super long error that ' + 'was thrown because of Batman.' + 'When you stop to think about ' + 'how Batman had anything to do ' + 'with this, you would get nowhere ' + 'fast.'; -
When programatically building up a string, use Array#join instead of string concatenation. Mostly for IE: jsPerf.
var items, messages, length, i; messages = [{ state: 'success', message: 'This one worked.' },{ state: 'success', message: 'This one worked as well.' },{ state: 'error', message: 'This one did not work.' }]; length = messages.length; // bad function inbox(messages) { items = '<ul>'; for (i = 0; i < length; i++) { items += '<li>' + messages[i].message + '</li>'; } return items + '</ul>'; } // good function inbox(messages) { items = []; for (i = 0; i < length; i++) { items[i] = '<li>' + messages[i].message + '</li>'; } return '<ul>' + items.join('') + '</ul>'; }
-
Function expressions:
// anonymous function expression var anonymous = function() { return true; }; // named function expression var named = function named() { return true; }; // immediately-invoked function expression (IIFE) (function() { console.log('Welcome to the Internet. Please follow me.'); })();
-
Never declare a function in a non-function block (if, while, etc). Assign the function to a variable instead. Browsers will allow you to do it, but they all interpret it differently, which is bad news bears.
// bad if (currentUser) { function test() { console.log('Nope.'); } } // good if (currentUser) { var test = function test() { console.log('Yup.'); }; }
-
Never name a parameter
arguments, this will take precendence over theargumentsobject that is given to every function scope.// bad function nope(name, options, arguments) { // ...stuff... } // good function yup(name, options, args) { // ...stuff... }
-
Use dot notation when accessing properties.
var luke = { jedi: true, age: 28 }; // bad var isJedi = luke['jedi']; // good var isJedi = luke.jedi;
-
Use subscript notation
[]when accessing properties with a variable.var luke = { jedi: true, age: 28 }; function getProp(prop) { return luke[prop]; } var isJedi = getProp('jedi');