diff --git a/manual/en-US/coding-standards/chapters/javascript.md b/manual/en-US/coding-standards/chapters/javascript.md index c6a09bf4..b27fde09 100644 --- a/manual/en-US/coding-standards/chapters/javascript.md +++ b/manual/en-US/coding-standards/chapters/javascript.md @@ -1,12 +1,34 @@ +## JavaScript + +### Contents + +1. [Naming Conventions](#naming-conventions) + - [Variables](#naming-conventions-variables) + - [Functions](#naming-conventions-functions) + - [Reserved Words](#naming-conventions-reserved) +2. [Syntax Style](#syntax-style) + - [Indentation](#syntax-indentation) + - [Spacing](#syntax-spacing) + - [Commas](#syntax-commas) + - [Semicolons](#syntax-semicolons) + - [Quotes](#syntax-quotes) +3. [Types](#types) +4. [Functions](#functions) +5. [Conditional Statements](#conditional-statements) +6. [Blocks & Multi-line Statements](#blocks) +7. [Comments](#comments) + + ## Naming Conventions Use descriptive words or terse phrases for names. Variables and Functions should be camel case, starting with a lowercase letter: `likeThis` -### Strings + +### Variables -**Use names that describe what the string is:** +**Use names that describe what the variable is:** `var element = document.getElementById('elementId');` @@ -14,6 +36,7 @@ Variables and Functions should be camel case, starting with a lowercase letter: Use i for index in a loop (and subsequent letters when necessary for nested iteration). + ### Functions **Use names that describe what the function does:** @@ -23,20 +46,22 @@ function getSomeData() { // statements } ``` - + ### Reserved Words Do not use reserved words for anything other than their intended use. The list of: [Reserved Words](http://es5.github.io/#x7.6.1) --- + ## Syntax Style + ### Indentation - Don't mix tabs and spaces. - Tabs, 4 spaces - + ### Spacing - No whitespace at the end of line or on blank lines. - Unary special-character operators (e.g., !, ++) must not have space next to their operand. @@ -117,7 +142,7 @@ foo( data, function() { }); ``` - + ### Commas **Place commas after:** @@ -141,7 +166,7 @@ array = [ 'foo', 'bar', ]; array = [ 'foo', 'bar' ]; ``` - + ### Semicolons Use them where expected. @@ -164,6 +189,7 @@ function foo() { } ``` + ### Quotes Use ' instead of " @@ -171,7 +197,7 @@ Use ' instead of " --- - + ## Variables ### Avoid Global Variables @@ -216,6 +242,7 @@ var foo = 'bar', baz = 'qux'; ``` + ## Types ### String @@ -233,10 +260,6 @@ var longString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' + 'cursus mauris.'; ``` -``` -var foo = 'bar'; -``` - ### Number Use `parseInt()` or `parseFloat()` instead of unary plus, for readability. @@ -319,6 +342,7 @@ var myArr = []; myArr.push('foo'); ``` + ## Functions ### Chaining Method Calls @@ -330,6 +354,7 @@ $('.someElement') .fadeIn(); ``` + ## Conditional Statements Use ternary syntax if: @@ -389,7 +414,7 @@ Use strict equality operator === so that type is considered in comparison. Using 1 === "1" ``` - + ## Blocks & Multi-line Statements Use curly braces on blocks that have more than one statement. @@ -409,7 +434,7 @@ if ( test ) { } ``` - + ## Comments **Single Line**