TheAlgorithms/JavaScript · error · TypeError

The given value is not a string

Error message

The given value is not a string

What it means

Thrown as a TypeError by checkPangramRegex() when the input is not a string. The function runs string.match(/([a-z])(?!.*\1)/gi) and reads .length, so a non-string receiver would fail or coerce misleadingly. Note: even for valid strings, if the string contains no letters at all, match() returns null and .length throws a separate TypeError — the input guard does not protect against that.

Source

Thrown at String/CheckPangram.js:16

/**
 * What is Pangram?
 * Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram
 */

/**
 * @function checkPangramRegex
 * @description - This function check pangram with the help of regex pattern
 * @param {string} string
 * @returns {boolean}
 * @example - checkPangramRegex("'The quick brown fox jumps over the lazy dog' is a pangram") => true
 * @example - checkPangramRegex('"Waltz, bad nymph, for quick jigs vex." is a pangram') => true
 */
const checkPangramRegex = (string) => {
  if (typeof string !== 'string') {
    throw new TypeError('The given value is not a string')
  }

  /**
   * Match all 26 alphabets using regex, with the help of:
   * Capturing group - () -> Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.
   * Character set - [a-z] -> Matches a char in the range a to z in case-insensitive for the 'i' flag
   * Negative lookahead - (?!) -> Specifies a group that can not match after the main expression (if it matches, the result is discarded).
   * Dot - . -> Matches any character except linebreaks. Equivalent to
   * Star - * -> Matches 0 or more of the preceding token.
   * Numeric reference - \{$n} -> Matches the results of a capture group. E.g. - \1  matches the results of the first capture group & \3 matches the third.
   */
  return string.match(/([a-z])(?!.*\1)/gi).length === 26
}

/**
 * @function checkPangramSet
 * @description - This function detect the pangram sentence by HashSet
 * @param {string} string

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Coerce: checkPangramRegex(String(s)).
  2. For the null-match risk, ensure the string contains at least one letter before calling, or wrap in try/catch.
  3. Prefer checkPangramSet (error 139) for strings that may have no letters, since it uses a Set and does not dereference null.

Example fix

// before
checkPangramRegex(sentence)

// after
const s = String(sentence ?? '')
const isPangram = /[a-z]/i.test(s) ? checkPangramRegex(s) : false
Defensive patterns

Strategy: type-guard

Validate before calling

function safeCheckPangramRegex(s) {
  if (typeof s !== 'string') {
    throw new TypeError('Expected a string')
  }
  if (!/[a-z]/i.test(s)) return false // avoid null match downstream
  return checkPangramRegex(s)
}

Type guard

function isStringWithLetters(v) {
  return typeof v === 'string' && /[a-z]/i.test(v)
}

Try / catch

try {
  checkPangramRegex(sentence)
} catch (e) {
  if (e instanceof TypeError) {
    return false
  }
  throw e
}

Prevention

When it happens

Trigger: Calling checkPangramRegex(null), checkPangramRegex(undefined), checkPangramRegex(123), or checkPangramRegex(['a','b']). Separately, checkPangramRegex('123!@#') passes the guard but throws on null.match().length downstream.

Common situations: Sentence fields that are sometimes null; numeric-only input mistaken for text; stripped strings that lost all alphabetic content.

Related errors


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