
20 JavaScript Modernizations
The Javascript ecosystem is progressing rapidly. However, it is hard to keep codebases up-to-date with the newer JavaScript features, and codemods are not always an option due to their significant churn and potential for breakages. The JS Assistant supports both codemod-like mass code refactoring and more opportunistic code modernization for the following upgrades:
- Add numeric separator
Increase the readability of long numbers and uncommon number formats by adding underscore separators.
- Collapse object property into shorthand
Shorten object properties when the property name is the same as the property value.
- Convert .apply() to spread syntax
Replace
.apply()
calls with the spread operator...
- Convert array.indexOf() into array.includes()
Replace
array.indexOf()
checks witharray.includes()
. - Convert string comparison chain to array.includes()
Replace
|| value === 'aString'
and&& value !== 'aString'
chains witharray.includes()
. - Convert function to arrow function
Replace function expressions with arrow functions, a more concise syntax.
- Convert function to object method
Convert property assignments with functions to method declarations.
- Convert loop to for…of
Replace regular
for
loops andanArray.forEach
loops withfor…of
loops. - Convert Math.pow to exponentiation operator
Use the exponentiation operator
**
instead ofMath.pow()
. - Convert string to template literal
Convert a string to a basic template literal without expressions.
- Convert to destructuring assignment
Convert a variable declaration that accesses an object property to a destructuring assignment.
- Convert to optional chaining
Replace various guard expressions with the optional chaining operator (
?.
). - Move default value into parameter
Replace default value assignment expressions with default parameter values.
- Convert var to let or const
Replace
var
with block-scoped variableslet
andconst
. - Replace void 0 with undefined
Replace
void 0
and other constantvoid
expressions withundefined
. - Use == null comparison
Replace different nullish checks with
== null
. - Use nullish coalescence in default expression
Replace default value expression with nullish coalescing operator (
??
) expressions. - Use string.endsWith()
string.endsWith()
checks if a string ends with another string. - Use string.startsWith()
string.startsWith()
checks if a string starts with another string. - Merge string concatenation
Merge string and template literal concatenation into a single template literal or string.