Skip to main content

How to Resolve "TypeError: .startsWith is not a function" Error in JavaScript

The TypeError: x.startsWith is not a function is a common error that occurs when you try to call the String.prototype.startsWith() method on a value that is not a string. This typically happens when you mistakenly assume a variable holds a string, but it actually holds a number, an object, null, or undefined.

This guide will explain the common causes of this error and show you the correct, modern solutions for both guarding against the error and correctly performing a check on your data.

The Core Problem: .startsWith() is a String-Only Method

The .startsWith() method is a function that exists exclusively on the String.prototype. It is designed to check if a string begins with the characters of another string. It does not exist on any other data type. When you try to call it on a number, an array, a plain object, null, or undefined, JavaScript throws a TypeError.

Example of problem:

// Problem: `myData` is a number, not a string.
let myData = 12345;

// ⛔️ TypeError: myData.startsWith is not a function
let result = myData.startsWith('123');
note

The key to solving this error is to ensure you are only calling .startsWith() on a true string.

Cause 1 (Most Common): The Variable is a Number

This is a very frequent source of the error, especially when working with IDs, codes, or numerical data.

Example of problem:

// Problem: `postalCode` is a number, not a string.
let postalCode = 90210;

// ⛔️ TypeError: postalCode.startsWith is not a function
let isLocal = postalCode.startsWith('90');

Solution: you must first convert the number to a string before you can call a string method on it. The String() constructor is the simplest way to do this.

let postalCode = 90210;

// ✅ Correct: First, convert the number to a string.
let postalCodeString = String(postalCode);

let isLocal = postalCodeString.startsWith('90');
console.log(isLocal); // Output: true

Cause 2: The Variable is an Object or another Non-String Type

You might have an object and try to call .startsWith() on it directly, when your intent was to check one of its properties.

Example of problem:

// Problem: `user` is an object.
let user = { username: 'test-user', id: 1 };

// ⛔️ TypeError: user.startsWith is not a function
user.startsWith('test');

Solution: you must access the specific string property on the object that you intend to test.

// ✅ Correct: Call `startsWith` on the string property.
let user = { username: 'test-user', id: 1 };
let isTestUser = user.username.startsWith('test');

console.log(isTestUser); // Output: true

Cause 3: The Variable is undefined or null

If a variable has not been initialized or a function did not return a value, it may be undefined.

Example of problem:

let myData; // `myData` is `undefined`

// ⛔️ TypeError: Cannot read properties of undefined (reading 'startsWith')
myData.startsWith('a');

Solution: guard your code to ensure you only call startsWith on a valid string. Optional chaining (?.) is a modern and concise way to do this.

let myData; // `myData` is `undefined`

// ✅ Safe: If `myData` is null or undefined, the expression returns `undefined`.
let result = myData?.startsWith('a');

console.log(result); // Output: undefined (no error thrown)

You can combine this with the nullish coalescing operator (??) to provide a default boolean value.

// If the result is `undefined` (because the variable was nullish), fall back to `false`.
let safeResult = myData?.startsWith('a') ?? false;
console.log(safeResult); // Output: false

Practical Example: A Robust safeStartsWith Function

This function demonstrates how to safely perform a startsWith check, handling different data types gracefully.

function safeStartsWith(data, searchString) {
// Guard against null and undefined
if (data == null) {
return false;
}

// Convert numbers to strings before checking
let stringToTest = String(data);

return stringToTest.startsWith(searchString);
}

// Example Usage:
console.log(safeStartsWith('hello', 'he')); // Output: true
console.log(safeStartsWith(12345, '12')); // Output: true
console.log(safeStartsWith(null, 'a')); // Output: false
console.log(safeStartsWith({ a: 1 }, 'a')); // Output: false

Conclusion

The TypeError: .startsWith is not a function is a clear signal that you are trying to use a string method on the wrong data type.

  • Remember that .startsWith() is only available for strings.
  • If you have a number, you must convert it to a string first: String(myNum).startsWith(...).
  • If you have an object, you must access one of its properties that is a string: myObj.myProp.startsWith(...).
  • Use optional chaining (?.) to prevent errors when a variable might be null or undefined.