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.