TheAlgorithms/JavaScript · error · Error

The arg must be a valid, non empty string

Error message

The arg must be a valid, non empty string

What it means

Thrown by permutate() in String/PermutateString.js when its single argument is not a string, or is the empty string ''. The guard combines a typeof check with a truthiness check, so both non-string inputs (numbers, objects, undefined) and the empty string are rejected before the .split('') permutation algorithm runs. The library refuses to compute permutations of nothing.

Source

Thrown at String/PermutateString.js:5

'use strict'

const permutate = (aString) => {
  if (typeof aString !== 'string' || !aString) {
    throw new Error('The arg must be a valid, non empty string')
  }
  const characters = aString.split('')
  let permutations = [[characters.shift()]]
  while (characters.length) {
    const currentCharacter = characters.shift()
    permutations = calculateCurrentCharacterPermutation(
      permutations,
      currentCharacter
    )
  }
  return permutations
    .map((character) => character.join(''))
    .filter((item, index, self) => self.indexOf(item) === index)
    .sort()
}

const calculateCurrentCharacterPermutation = (
  allPermutations,

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Pass a non-empty string literal, e.g. permutate('abc').
  2. Coerce or validate upstream: if (typeof input === 'string' && input.length) { permutate(input) }.
  3. Default the argument with a guard: permutate(input ?? '') will still throw on '', so supply a real fallback string instead.
  4. If the caller genuinely may have non-string data, normalize first: permutate(String(input)) only when input is non-null/defined and not empty.

Example fix

// before
const result = permutate(userInput) // userInput may be undefined or ''

// after
if (typeof userInput === 'string' && userInput.length > 0) {
  const result = permutate(userInput)
} else {
  throw new Error('userInput missing')
}
Defensive patterns

Strategy: validation

Validate before calling

function permutateSafe(input) {
  if (typeof input !== 'string' || input.length === 0) {
    throw new TypeError('permutate requires a non-empty string')
  }
  return permutate(input)
}

Type guard

function isNonEmptyString(v) {
  return typeof v === 'string' && v.length > 0
}

Try / catch

try {
  const perms = permutate(maybeInput)
} catch (err) {
  if (/non empty string/.test(err.message)) {
    // handle bad input
  } else throw err
}

Prevention

When it happens

Trigger: Calling permutate(123), permutate(undefined), permutate(null), permutate(['a','b']), permutate({}), or permutate(''). Any value where typeof aString !== 'string' OR the string coerces to falsy (only '') triggers the throw at line 5.

Common situations: Reading input from process.argv or a form field that arrives as undefined when omitted; passing a parsed JSON value that was a number; forgetting to default a text input and forwarding an empty string; unit-testing edge cases and supplying '' to mean 'no input'.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/5fbc59afaddcc13d. Report an issue: GitHub.