Categories
JavaScript Answers

How to fix JavaScript error “val.match is not a function”?

Spread the love

Sometimes, we want to fix JavaScript error "val.match is not a function"

In this article, we’ll look at how to fix JavaScript error "val.match is not a function".

How to fix JavaScript error "val.match is not a function"?

To fix JavaScript error "val.match is not a function", we should make sure val is a string.

For instance, we write

const val = 12;

if (String(val).match(/^s+$/) || val === "") {
  console.log("success: ", val);
}

to call String with val to convert it to a string.

Then we call match on the returned string.

Conclusion

To fix JavaScript error "val.match is not a function", we should make sure val is a string.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *