13 Noteworthy Points from Google’s JavaScript Style Guide
Don’t use var anymore
Declare all local variables with either
<em class="markup--em markup--pullquote-em"><span style="color: #000000;">const</span></em>
or<em class="markup--em markup--pullquote-em"><span style="color: #000000;">let</span></em>
. Use const by default, unless a variable needs to be reassigned. The<em class="markup--em markup--pullquote-em"><span style="color: #000000;">var</span></em>
keyword must not be used.Arrow functions are preferred
Arrow functions provide a concise syntax and fix a number of difficulties with
<em class="markup--em markup--pullquote-em"><span style="color: #000000;">this</span></em>
. Prefer arrow functions over the<em class="markup--em markup--pullquote-em"><span style="color: #000000;">function</span></em>
keyword, particularly for nested functionsUse template strings instead of concatenation
Use template strings (delimited with
<span style="color: #000000;">`</span>
) over complex string concatenation, particularly if multiple string literals are involved. Template strings may span multiple lines.// bad function sayHi(name) { return 'How are you, ' + name + '?'; } // bad function sayHi(name) { return ['How are you, ', name, '?'].join(); } // bad function sayHi(name) { return `How are you, ${ name }?`; } // good function sayHi(name) { return `How are you, ${name}?`; }“for… of” is the preferred type of ‘for loop’
With ES6, the language now has three different kinds of
<span style="color: #000000;">for</span>
loops. All may be used, though<span style="color: #000000;">for</span>
–<span style="color: #000000;">of</span>
loops should be preferred when possible.Constants should be named in ALL_UPPERCASE separated by underscores
Constant names use
<span style="color: #000000;">CONSTANT_CASE</span>
: all uppercase letters, with words separated by underscores.