Google JavaScript Style Guide

Revision 2.93

Aaron Whyte
Bob Jervis
Dan Pupius
Erik Arvidsson
Fritz Schneider
Robby Walker

Each style point has a summary for which additional information is available by toggling the accompanying arrow button that looks this way: ▶. You may toggle all summaries with the big arrow button:

▶ Toggle all summaries
Table of Contents

Important Note

Displaying Hidden Details in this Guide

link ▶
This style guide contains many details that are initially hidden from view. They are marked by the triangle icon, which you see here on your left. Click it now. You should see "Hooray" appear below.

Background

JavaScript is the main client-side scripting language used by many of Google's open-source projects. This style guide is a list of dos and don'ts for JavaScript programs.

JavaScript Language Rules

var

link ▶
Declarations with var: Always

Constants

link ▶
  • Use NAMES_LIKE_THIS for constant values.
  • Use @const to indicate a constant (non-overwritable) pointer (a variable or property).
  • Never use the const keyword as it's not supported in Internet Explorer.

Semicolons

link ▶
Always use semicolons.

Nested functions

link ▶
Yes

Function Declarations Within Blocks

link ▶
No

Exceptions

link ▶
Yes

Custom exceptions

link ▶
Yes

Standards features

link ▶
Always preferred over non-standards features

Wrapper objects for primitive types

link ▶
No

Multi-level prototype hierarchies

link ▶
Not preferred

Method and property definitions

link ▶
/** @constructor */ function SomeConstructor() { this.someProperty = 1; } Foo.prototype.someMethod = function() { ... };

delete

link ▶
Prefer this.foo = null.

Closures

link ▶
Yes, but be careful.

eval()

link ▶
Only for code loaders and REPL (Read–eval–print loop)

with() {}

link ▶
No

this

link ▶
Only in object constructors, methods, and in setting up closures

for-in loop

link ▶
Only for iterating over keys in an object/map/hash

Associative Arrays

link ▶
Never use Array as a map/hash/associative array

Multiline string literals

link ▶
No

Array and Object literals

link ▶
Yes

Modifying prototypes of builtin objects

link ▶
No

Internet Explorer's Conditional Comments

link ▶
No

JavaScript Style Rules

Naming

link ▶

In general, use functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, CONSTANT_VALUES_LIKE_THIS, foo.namespaceNamesLikeThis.bar, and filenameslikethis.js.

Custom toString() methods

link ▶
Must always succeed without side effects.

Deferred initialization

link ▶
OK

Explicit scope

link ▶
Always

Code formatting

link ▶
Expand for more information.

Parentheses

link ▶
Only where required

Strings

link ▶
Prefer ' over "

Visibility (private and protected fields)

link ▶
Encouraged, use JSDoc annotations @private and @protected

JavaScript Types

link ▶
Encouraged and enforced by the compiler.

Comments

link ▶
Use JSDoc

Providing Dependencies With goog.provide

link ▶
Only provide top-level symbols.

Compiling

link ▶
Required

Tips and Tricks

link ▶
JavaScript tidbits

Parting Words

BE CONSISTENT.

If you're editing code, take a few minutes to look at the code around you and determine its style. If they use spaces around all their arithmetic operators, you should too. If their comments have little boxes of hash marks around them, make your comments have little boxes of hash marks around them too.

The point of having style guidelines is to have a common vocabulary of coding so people can concentrate on what you're saying rather than on how you're saying it. We present global style rules here so people know the vocabulary, but local style is also important. If code you add to a file looks drastically different from the existing code around it, it throws readers out of their rhythm when they go to read it. Avoid this.

Revision 2.93

Aaron Whyte
Bob Jervis
Dan Pupius
Erik Arvidsson
Fritz Schneider
Robby Walker