ECMAScript is the standard behind JavaScript. ECMAScript 6 (sometimes, incorrectly, referred to as JavaScript 6.0) is a significant update - arguably the most significant in the history of the language - and introduces a lot of new syntax, types, and capabilities. While most browsers do not yet support ECMAScript 6, Ignia is developing for it today using transpilers such as Google Traceur and Babel.
Note: This style guide inherits rules from the JavaScript Style Guide, C-Based Languages Style Guide, and the Global Style Guide.
Note: The ECMAScript 6 standard is not yet finalized, and some implementation details are expected to change before publication; the guidance below will be reevaluated at that point to ensure consistency with the final specification.
- Prefer
includes()
,startsWith()
andendsWith()
toindexOf()
when operating against strings and the index of the match is not needed - Prefer template strings (e.g.,
`By ${name}`
) to string concatenation (e.g.,'By ' + name
) - Prefer using
object.is()
to===
for evaluating identicality; it is more readable, and mitigates edge cases withNaN
,-0
and+0
- Only use the
u
flag when performing regular expressions on strings including 16-bit characters; it is not performant
- Always use
let
instead ofvar
; it follows more predictable scoping and hoisting rules, particularly in loops, and prevents accidental redeclaration of global variables - Always use
set
when defining genuine constants - Consider using array destructuring as a more readable alternative to hard-coding indexes (e.g.,
array[3]
) if the values are not expected to change within the variable scope; e.g.,
let users = [['Jeremy', 'Caney', 'Architect'], ['Katherine', 'Trunkey', 'Developer']];
for (let i=0; i < users.length; i++) {
let [firstName, lastName, title] = users[i];
console.log(lastName + ", " + firstName + "(" + title + ")");
}
- Prefer use of symbols (e.g.,
Symbol('private')
) to naming conventions (e.g.,_private
) to hide members fromgetOwnPropertyNames()
- Use shared symbols (e.g.,
Symbol.set('internal')
) for symbols that need to be accessed across multiple classes (e.g., "internal")
- Use shared symbols (e.g.,
- Consider using
Proxy
classes when specific business logic needs to be applied to all properties in a class (e.g., for validation, such as type safety), potentially including properties not statically defined (e.g., dynamic evaluation instantiation) - Prefer
Object.assign()
to framework-specific mixin or extends methods (e.g.,angular.extend()
orangular.merge()
) - When defining objects using object literal syntax, prefer the new method intializer shorthand (e.g.,
getValue() {}
) instead of function expressions (e.g.,getValue: function() {}
) - Use
super
instead ofObject.getPrototypeOf(this)
to call the base method from the object's prototype
- If using destructured parameters, always provide a default, even if it's just an empty object; otherwise, an error will be thrown if the object is not passed; e.g.,
addBook({author, title, datePublished} = {}, autoSave = true) {
...
}
- Make use of default parameters in method calls to clearly communicate what is optional, and to simplify setting defaults
Note: Default parameters are only defined if a variable is undefined; it may still be necessary, therefore, to check for
null
values
- Prefer use of rest parameters (i.e.,
...array
) over theparameters
collection, as it is more explicit - For handling option parameters, prefer use of
Object.assign()
over destructured parameters, as this keeps variables maintained in an object - Avoid passing anonymous function expressions as callbacks; instead, use either arrow functions (e.g.,
(param1, param1) => param1 + param2
) or references to named functions
- If a collection contains unique items and there is no need to retrieve (or set) individual records by an indexer, prefer
Set
overArray
- For associative arrays (e.g., dictionaries) prefer
Map
overObject
; these allow non-string keys (e.g., integers, objects), honor entry order, and allow for members to be defined independent of values (e.g., to add afilter()
method) - Prefer
for-of
style iteration to traditionalfor
loops; with iterators, the behavior is standardized acrossArray
,Object
,Set
,Map
, and evenNodeList
Note: When relying on transpilers for backward compatibility support, carefully test new language features to ensure they are handled correctly; this is especially true for syntactical changes.