How to determine the type of a character wrapped in quotes?
I am retrieving params from a URL that needs to be evaluated through a type test to be used in a pagination service:
http://192.168.1.8:4200/posts?page=g
I want to redirect to another path if user puts anything other than number as a page param
I have tried using if(typeof JSON.parse(queries.page)===number)
it works in case of a number input, but does not get through in case of a string for example:
http://192.168.1.8:4200/posts?page=g
it generates an error of:
VM873:1 Uncaught SyntaxError: Unexpected token 'g', "g" is not valid JSON at JSON.parse () at :1:14
The answer is using isNaN as an identifier:
I can use
isNaN()instead oftypeof()if(isNaN(+queries.page))returns
truehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN