TheAlgorithms/JavaScript · error · TypeError

Argument is not a string.

Error message

Argument is not a string.

What it means

Thrown as a TypeError by checkCamelCase() when varName is not a string. The function tests varName against the regex /^[a-z][A-Za-z]*$/ via RegExp.prototype.test, which coerces non-strings but the library chooses to reject them explicitly to avoid surprising truthy results (e.g. numbers coercing to strings).

Source

Thrown at String/CheckCamelCase.js:13

// CheckCamelCase method checks the given string is in camelCase or not.

// Problem Source & Explanation: https://en.wikipedia.org/wiki/Camel_case

/**
 * checkCamelCase method returns true if the string in camelCase, else return the false.
 * @param {String} varName the name of the variable to check.
 * @returns `Boolean` return true if the string is in camelCase, else return false.
 */
const checkCamelCase = (varName) => {
  // firstly, check that input is a string or not.
  if (typeof varName !== 'string') {
    throw new TypeError('Argument is not a string.')
  }

  const pat = /^[a-z][A-Za-z]*$/
  return pat.test(varName)
}

export { checkCamelCase }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Coerce: checkCamelCase(String(name)).
  2. Guard: if (typeof name === 'string') checkCamelCase(name).
  3. Default the parameter at the call site: checkCamelCase(name ?? '').

Example fix

// before
const ok = checkCamelCase(node.name) // node.name may be undefined

// after
const ok = typeof node.name === 'string' && checkCamelCase(node.name)
Defensive patterns

Strategy: type-guard

Validate before calling

function safeCheckCamelCase(name) {
  if (typeof name !== 'string') {
    throw new TypeError('Expected a string')
  }
  return checkCamelCase(name)
}

Type guard

function isString(v) {
  return typeof v === 'string'
}

Try / catch

try {
  checkCamelCase(name)
} catch (e) {
  if (e instanceof TypeError && e.message === 'Argument is not a string.') {
    return false
  }
  throw e
}

Prevention

When it happens

Trigger: Calling checkCamelCase(null), checkCamelCase(undefined), checkCamelCase(123), checkCamelCase(['myVar']), or checkCamelCase({name: 'myVar'}).

Common situations: Validating identifiers sourced from JSON where the field is absent; AST node names that may be numbers; form input not yet coerced.

Related errors


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