TypeError: startsWith 不是 JavaScript 中的函数

TypeError: startsWith 不是 JavaScript 中的函数

TypeError: startsWith is not a function in JavaScript

“TypeError: startsWith is not a function”错误发生在我们
startsWith()对一个不是字符串的值调用方法时。

要解决该错误,请使用该toString()
方法将值转换为字符串,或确保仅对
startsWith字符串调用该方法。

typeerror startswith 不是一个函数

下面是错误如何发生的示例。

索引.js
const str = 123; // ⛔️ TypeError: startsWith is not a function const result = str.startsWith('1');

我们对一个数字调用了
String.startsWith
方法并返回了错误。

startsWith只在字符串上调用方法

要解决该错误,请确保仅对startsWith()字符串调用该方法。您可以使用toString()方法将大多数值转换为字符串。

索引.js
const str = 123; const result = str.toString().startsWith('1'); console.log(result); // 👉️ true

您还可以使用String()构造函数将值转换为字符串。

索引.js
const str = 123; const result = String(str).startsWith('1'); console.log(result); // 👉️ true

构造String()函数将提供的值转换为字符串并返回结果。

string在调用之前检查值是否是类型startsWith()

startsWith()或者,您可以在调用该方法之前检查该值是否为字符串

索引.js
const str = null; const result = typeof str === 'string' ? str.startsWith('a') : false; console.log(result); // 👉️ false

我们使用三元运算符来检查str变量是否存储字符串。

如果是,则返回逗号左边的值,否则返回右边的值。

您也可以使用简单的if语句来获得相同的结果。

索引.js
const str = null; let result = false; if (typeof str === 'string') { result = str.startsWith('a'); } console.log(result); // 👉️ false
If the value is a string, we return the result of calling the startsWith method, otherwise, we return false.

If the error persists, console.log the value you’re calling the startsWith
method on and check its type using the typeof operator, e.g.
console.log(typeof myStr).

index.js
console.log(typeof 'bobbyhadz.com'); // 👉️ string console.log(typeof {}); // 👉️ object console.log(typeof 123); // 👉️ number

If the value is an object, there’s a very good chance that you are forgetting to
access a specific property on which you need to call the startsWith() method.

index.js
// ✅ with objects const obj = { name: 'bobby', }; const result1 = obj.name.startsWith('bo'); console.log(result1); // 👉️ true // ----------------------------------------- // ✅ with arrays const arr = ['bobby', 'hadz', 'com']; const result2 = arr[0].startsWith('bo'); console.log(result2); // 👉️ true

We accessed a property on the object and an element in the array before calling
the startsWith() method.

Check if all strings in an array start with a specific substring #

If you want to check if all strings in an array start with a specific substring,
use the every() method to iterate over the array and then call the
startsWith method on each string.

index.js
const arr = ['ab', 'ac', 'ad']; const result = arr.every(str => str.startsWith('a')); console.log(result); // 👉️ true
The every() method takes a function and calls the function with each element in the array until the function returns a falsy value or iterates over the entire array.

On each iteration, we call the startsWith method on the string and return the
result.

Conclusion #

The “TypeError: startsWith is not a function” error occurs when we call the
startsWith() method on a value that is not a string.

To solve the error, convert the value to a string using the toString()
method or make sure to only call the startsWith method on strings.