TheAlgorithms/JavaScript · error · TypeError
Invalid phone number!
Error message
Invalid phone number!
What it means
Guard in formatPhoneNumber: it throws when phoneNumber.length !== 10 OR isNaN(phoneNumber). The function expects a STRING of exactly 10 numeric digits and rebuilds the '(XXX) XXX-XXXX' pattern by indexing into the string. Because it reads .length, a Number has no length (undefined !== 10) and throws; because it uses the coercion-based isNaN, a string of 10 spaces incorrectly passes the numeric check (latent bug). Empty or wrong-length strings throw.
Source
Thrown at String/FormatPhoneNumber.js:9
/**
* @description - function that takes 10 digits and returns a string of the formatted phone number e.g.: 1234567890 -> (123) 456-7890
* @param {string} phoneNumber
* @returns {string} - Format to (XXX) XXX-XXXX pattern
*/
const formatPhoneNumber = (phoneNumber) => {
if (phoneNumber.length !== 10 || isNaN(phoneNumber)) {
// return "Invalid phone number."
throw new TypeError('Invalid phone number!')
}
let index = 0
return '(XXX) XXX-XXXX'.replace(/X/g, () => phoneNumber[index++])
}
export default formatPhoneNumber
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Pass a string of exactly 10 digits: strip non-digits first, e.g. String(n).replace(/\D/g, '').
- Validate length === 10 and /^[0-9]{10}$/.test(value) before calling.
- Prefer Number.isFinite over the library's isNaN if you fork/patch, to close the whitespace hole.
Example fix
// before
formatPhoneNumber(rawPhone)
// after
const digits = String(rawPhone).replace(/\D/g, '')
if (/^[0-9]{10}$/.test(digits)) formatPhoneNumber(digits) Defensive patterns
Strategy: validation
Validate before calling
const digits = String(phoneNumber).replace(/\D/g, '')
if (!/^[0-9]{10}$/.test(digits)) {
throw new TypeError('phoneNumber must be exactly 10 digits')
}
formatPhoneNumber(digits) Type guard
const isTenDigitString = (v) => typeof v === 'string' && /^[0-9]{10}$/.test(v) Try / catch
try {
formatPhoneNumber(raw)
} catch (e) {
if (e instanceof TypeError && /Invalid phone number/i.test(e.message)) {
// sanitize and retry, or surface to user
} else throw e
} Prevention
- Always strip non-digits first: String(n).replace(/\D/g, '').
- Never pass a Number — it has no .length.
- Beware the isNaN coercion hole: validate with a strict regex, not length+isNaN.
- Reject formatted input (dashes/parens) before calling.
When it happens
Trigger: Calling formatPhoneNumber(1234567890) (number, no .length), formatPhoneNumber('123') (too short), formatPhoneNumber('12345678901') (too long), formatPhoneNumber('12345678ab') (non-numeric), formatPhoneNumber('') (length 0), formatPhoneNumber(null) (throws on .length access, possibly a different error).
Common situations: Receiving a phone number as a Number from a form/database instead of a string; formatted input with dashes/parens already applied (length > 10); stripped input that retained a country code; whitespace-only input slipping through due to the isNaN coercion bug.
Related errors
- Argument is not a string.
- Argument is not a string.
- The first param should be a string
- The second param should be a boolean
- Input should be a string
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/5f1f01585511d89f.
Report an issue: GitHub.