Does anyone know how can I check whether a variable is a number or a string in JavaScript?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Check if the value is a string literal or String object:
Unit test:
Checking for a number is similar:
Errr? Just use regular expressions! :)
What do you thing about this one?
typeof works very well for me in most case. You can try using an if statement
where x is any variable name of your choice
Best way to do that is using isNaN + type casting:
Updated all-in method:
The same using regex:
Best way to do this:
This satisfies the following test cases: