JavaScript error: "val.match is not a function" JavaScript error: "val.match is not a function" javascript javascript

JavaScript error: "val.match is not a function"


I would say that val is not a string.

I get the "val.match is not function" error for the following

var val=12; if(val.match(/^s+$/) || val == ""){   document.write("success: " + val);}

The error goes away if you explicitly convert to a string String(val)

var val=12; if(String(val).match(/^s+$/) || val == ""){   document.write("success: " + val);}

And if you do use a string you don't need to do the conversion

var val="sss"; if(val.match(/^s+$/) || val == ""){   document.write("success: " + val);}


the problem is: val is not string

i can think of two options1) convert to string: might be a good option if you are sure val has to be string

"Same as above answer"

var val=12; if(String(val).match(/^s+$/) || val == ""){   document.write("success: " + val);}