JavaScript

JavaScript

Made by DeepSource

Found complex boolean return JS-W1041

Anti-pattern
Major

The following pattern:

if (condition) {
  return true
}
return false

can be refactored to:

return condition

When condition is not a boolean value, it can be cast into one using the Boolean constructor.

Bad Practice

function isEven(num: number) {
  if (num % 2 === 0) return true
  return false
}

async function userExists(name: string) {
  if (await db.getUser(name)) return true
  return false
}

Recommended

function isEven(num: number) {
  return num % 2 === 0
}

async function userExists(name: string) {
  return Boolean(await db.getUser(name))
}