Javascript
Javascript
Contents ii
1 Introduction 1
2 Coding Style 2
2.1 Purpose of this section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 Logic Guidelines 4
3.1 Async Await Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
ii
Chapter 1
Introduction [Intro.Scope]
(1)
The language in this document follows the ISO Directive standards as such:
1
Chapter 2
(2)
Example:
const bar = {
f o o : ’bar’ ,
f o o : ’bafoo’
};
The problem with this code is that it confuses what the definition of foo is in the literal.
The browser will only keep the last value associated to that key in the literal.
2
6 Globally visible page logic should be in a namespace.
Putting externally visible logic in a namespace reduces the likelihood that there will be a
conflict with other code. Moreover it makes it clear where a method originates and thus what
file owns it.
Example:
// Create bar namespace if it doesn’t
// exist or extend the existing one.
const bar = bar | | { } ;
( function ( barNS ) {
’use strict’ ;
barNS . f o o = {
// Declaration style for IE11
doSomething : function ( ) {
},
// Declaration style for newer browsers
doSomethingElse ( ) {
}
// extend bar.foo here
};
// Freeze the namespace so that
// it can’t be modified by other code
O b j e c t . f r e e z e ( barNS . f o o ) ;
} ) ( bar ) ;
Chapter 3
(3)
1 Logic in a file or page should be isolated in an Immediately Invoked Function Expression (IIFE).
Code running outside of an IIFE interacts with the global object, usually the window. This
means that any other code running on the page can modify any variables declared outside of a
closure created by an IIFE.
[ Note: This is not required in Lightning controllers, helpers, or renders. These run in isolated
closures by default. — end note ]
Example:
( function ( ) {
’use strict’ ;
// code goes here
})();
2 Logic should run in Strict mode to reduce the chance of developer errors.
Strict mode can prevent a lot of common mistakes in ECMAScript such as:
• Inadvertently assigning to the global object.
• Referencing an undeclared variable.
• Prevents silent unexpected failures.
[ Note: Lightning controllers, helpers, and renders all run in Strict mode if Locker Service is
enabled. See the Lightning standards for more information. — end note ]
3 In a chain of if statements, an else shall not be used if the prior if returns from the method.
4 Comparisons between two values shall use the === or ==! operators.
5 A developer should not make a comparison against null.
In most cases comparing against null is a mistake. By default JavaScript has multiple ways
of representing things that are not there. null is generally used to indicate that something was
there, or that a method failed in an expected way. As such, when checking to see if a value
exists, use the falsy check instead. In most cases, only directly check against null when there
is semantic meaning to the value.
6 Assignment shall not occur within a conditional.
7 isNaN() shall be the only method of checking for NaN values.
JavaScript’s NaN is based on the IEEE 754 standard. As such NaN never compares equal to
any value. Thus NaN !== NaN will always return true, and NaN === NaN will always return
false. The isNaN function is the only safe way to check if a value compares equal to NaN.
4
8 Empty blocks shall not be used
9 A developer should avoid unnecessary casts to Boolean.
Example:
// Use ES6 destructuring to get each
// result in an separate variable
const [ r e s u l t 1 , r e s u l t 2 , r e s u l t 3 ] =
a w a i t Promise . a l l ( [ promise1 , promise2 , p r o m i s e 3 ] ) ;
2 A developer shall not await a value they are returning.
awaiting a return value creates a performance hazard because it causes the runtime to pause
and resolve the awaited Promise. In an async method returning a Promise is fine as it will
replace the Promise the runtime would have generated as the return value. This allows the
method calling to choose how to await and prevents awaiting in a loop.
Source: ESLint: no-return-await.
Glossary
closure The function or lexical environment in which a variable is declared see: MDN Closures.
4
Strict mode A restricted variant of ECMAScript that makes it harder for Developers to make
mistakes see: MDN Strict Mode. 4