// bad const totalScore = this.reviewScore.toString(); // isn’t guaranteed to return a string
// good const totalScore = String(this.reviewScore);
Numbers: Use Number for type casting and parseInt always with a radix for parsing strings. eslint: radix no-new-wrappers
Why? The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading whitespace in string is ignored. If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X, in which case a radix of 16 is assumed. This differs from ECMAScript 3, which merely discouraged (but allowed) octal interpretation. Many implementations have not adopted this behavior as of 2013. And, because older browsers must be supported, always specify a radix.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
const inputValue = "4";
// bad const val = newNumber(inputValue);
// bad const val = +inputValue;
// bad const val = inputValue >> 0;
// bad const val = parseInt(inputValue);
// good const val = Number(inputValue);
// good const val = parseInt(inputValue, 10);
If for whatever reason you are doing something wild and parseInt is your bottleneck and need to use Bitshift for performance reasons, leave a comment explaining why and what you’re doing.
1 2 3 4 5 6 7
// good /** * parseInt was the reason my code was slow. * Bitshifting the String to coerce it to a * Number made it a lot faster. */ const val = inputValue >> 0;
Booleans
1 2 3 4 5 6 7 8 9 10
const age = 0;
// bad const hasAge = newBoolean(age);
// good const hasAge = Boolean(age);
// best const hasAge = !!age;
Naming Conventions
Don’t save references to this. Use arrow functions or Function#bind.
// bad functionfoo() { const that = this; returnfunction () { console.log(that); }; }
// good functionfoo() { return() => { console.log(this); }; }
You may optionally uppercase a constant only if it (1) is exported, (2) is a const (it can not be reassigned), and (3) the programmer can trust it (and its nested properties) to never change.
What about all const variables? - This is unnecessary, so uppercasing should not be used for constants within a file. It should be used for exported constants however.
What about exported objects? - Uppercase at the top level of export (e.g. EXPORTED_OBJECT.key) and maintain that all nested properties do not change.
Why? This is an additional tool to assist in situations where the programmer would be unsure if a variable might ever change. UPPERCASE_VARIABLES are letting the programmer know that they can trust the variable (and its properties) not to change.