Javascript
Recipes
- Disallow awaiting non-promise values.
- Disallow classes that only have static members.
- Disallow comparing
undefined
usingtypeof
. - Disallow else blocks after return statements in if statements
- Disallow equal signs explicitly at the beginning of regular expressions
- Disallow if statements as the only statement in else blocks
- Disallow initializing variables to undefined
- Disallow member access from
await
expression. - Disallow negated conditions.
- Disallow nested ternary expressions.
- Disallow
new Array()
. - Disallow number literals with zero fractions or dangling dots.
- Disallow
parseInt()
andNumber.parseInt()
in favor of binary, octal, and hexadecimal literals - Disallow redundant return statements
- Disallow renaming import, export, and destructured assignments to the same name
- Disallow returning/yielding
Promise.resolve()
/reject()
inasync
functions or promise callbacks. - Disallow shorthand type conversions
- Disallow ternary operators when simpler alternatives exist
- Disallow the use of
Math.pow
in favor of the ** operator - Disallow the use of the
null
literal. - Disallow unnecessary calls to
.bind()
- Disallow unnecessary computed property keys in objects and classes
- Disallow unnecessary labels
- Disallow unnecessary spread.
- Disallow unreadable array destructuring.
- Disallow use of
Object.prototype.hasOwnProperty.call()
and prefer use ofObject.hasOwn(
) - Disallow useless array
length
check. - Disallow useless fallback when spreading in object literals.
- Disallow useless
undefined
. - Disallow using
Object.assign
with an object literal as the first argument and prefer the use of object spread instead - Disallow using the
this
argument in array methods. - Do not use a
for
loop that can be replaced with afor-of
loop. - Do not use leading/trailing space between
console.log
parameters. - Enforce a convention in module import order
- Enforce a newline after import statements
- Enforce a particular style for multiline comments
- Enforce a specific parameter name in catch clauses.
- Enforce better string content.
- Enforce combining multiple
Array#push()
into one call. - Enforce consistent brace style for all control statements
- Enforce consistent brace style for case clauses.
- Enforce consistent case for text encoding identifiers.
- Enforce consistent relative URL style.
- Enforce correct
Error
subclassing. - Enforce dot notation whenever possible
- Enforce explicitly comparing the length or size property of a value.
- Enforce no spaces between braces.
- Enforce or ban the use of inline type-only markers for named imports
- Enforce or disallow capitalization of the first letter of a comment
- Enforce proper case for numeric literals.
- Enforce sorted import declarations within modules
- Enforce the style of numeric separators by correctly grouping digits.
- Enforce the use of
Buffer.from()
andBuffer.alloc()
instead of the deprecatednew Buffer()
. - Enforce the use of
Math.trunc()
instead of bitwise operators. - Enforce the use of Unicode escapes instead of hexadecimal escapes.
- Enforce the use of
new
for all builtins, exceptString
,Number
,Boolean
,Symbol
, andBigInt
. - Enforce throwing
TypeError
in type checking conditions. - Enforce using the digits argument with
Number#toFixed()
. - Enforce using the separator argument with
Array#join()
. - Enforce variables to be declared either together or separately in functions
- Ensure all imports appear before other statements
- Fix whitespace-insensitive template indentation.
- Forbid empty named import
- Forbid import of modules using absolute paths
- Forbid import statements with CommonJS module.exports
- Forbid importing packages through relative paths
- Forbid namespace (a.k.a. "wildcard"
*
) imports. - Forbid repeated import of the same module in multiple places
- Forbid unnecessary path segments in import and require statements
- Improve regexes by making them shorter, consistent, and safer.
- Prefer
.addEventListener()
and.removeEventListener()
over on-functions. - Prefer
.at()
method for index access andString#charAt()
. - Prefer
.before()
over.insertBefore()
,.replaceWith()
over.replaceChild()
, prefer one of.before()
,.after()
,.append()
or.prepend()
overinsertAdjacentText()
andinsertAdjacentElement()
. - Prefer
.find()
and.findLast()
over the first or last element from.filter()
. - Prefer
.flatMap()
over.map().flat()
. - Prefer
.includes()
over.indexOf()
andArray#some()
when checking for existence or non-existence. - Prefer
.querySelector()
over.getElementById()
,.querySelectorAll()
over.getElementsByClassName()
and.getElementsByTagName()
. - Prefer
.some()
over.filter().length
check and.{find,findLast}()
. - Prefer
Array#flat()
over legacy techniques to flatten arrays. - Prefer
Array#{indexOf,lastIndexOf}()
overArray#{findIndex,findLastIndex}()
when looking for the index of an item. - Prefer
Date.now()
to get the number of milliseconds since the Unix Epoch. - Prefer JavaScript modules (ESM) over CommonJS.
- Prefer
KeyboardEvent#key
overKeyboardEvent#keyCode
. - Prefer
Node#append()
overNode#appendChild()
. - Prefer
Number
static properties over global ones. - Prefer
Reflect.apply()
overFunction#apply()
. - Prefer
RegExp#test()
overString#match()
andRegExp#exec()
. - Prefer
Set#has()
overArray#includes()
when checking for existence or non-existence. - Prefer
String#replaceAll()
over regex searches with the global flag. - Prefer
String#slice()
overString#substr()
andString#substring()
. - Prefer
String#startsWith()
&String#endsWith()
overRegExp#test()
. - Prefer
String#trimStart()
/String#trimEnd()
overString#trimLeft()
/String#trimRight()
. - Prefer borrowing methods from the prototype instead of the instance.
- Prefer
childNode.remove()
overparentNode.removeChild(childNode)
. - Prefer default parameters over reassignment.
- Prefer
export…from
when re-exporting. - Prefer
for…of
over theforEach
method. - Prefer modern Math APIs over legacy patterns.
- Prefer negative index over
.length - index
when possible. - Prefer omitting the catch binding parameter.
- Prefer reading a JSON file as a buffer.
- Prefer
switch
over multipleelse-if
. - Prefer ternary expressions over simple
if-else
statements. - Prefer the spread operator over
Array.from()
,Array#concat()
,Array#{slice,toSpliced}()
andString#split('')
. - Prefer using
.dataset
on DOM elements over calling attribute methods. - Prefer using
Object.fromEntries()
to transform a list of key-value pairs into an object. - Prefer using
Set#size
instead ofArray#length
. - Prefer using
String
,Number
,BigInt
,Boolean
, andSymbol
directly. - Prefer using the
node:
protocol when importing Node.js builtin modules. - Prevent abbreviations.
- Require
Array.isArray()
instead ofinstanceof Array
. - Require braces around arrow function bodies
- Require const declarations for variables that are never reassigned after declared
- Require destructuring from arrays and/or objects
- Require escape sequences to use uppercase values.
- Require
let
orconst
instead ofvar
- Require
new
when throwing an error. - Require or disallow "Yoda" conditions
- Require or disallow Unicode byte order mark (BOM)
- Require or disallow assignment operator shorthand where possible
- Require or disallow logical assignment operator shorthand
- Require or disallow method and property shorthand syntax for object literals
- Require or disallow strict mode directives
- Require template literals instead of string concatenation
- Require the use of
===
and!==
- Require using arrow functions for callbacks
- Require variables within the same declaration block to be sorted
- Use destructured variables over properties.