Does anyone know how can I check whether a variable is a number or a string in JavaScript?
Does anyone know how can I check whether a variable is a number or a string in JavaScript?
The best way I have found is to either check for a method on the string, i.e.:
if (x.substring) {
// do string thing
} else{
// do other thing
}
or if you want to do something with the number check for a number property,
if (x.toFixed) {
// do number thing
} else {
// do other thing
}
This is sort of like "duck typing", it's up to you which way makes the most sense. I don't have enough karma to comment, but typeof fails for boxed strings and numbers, i.e.:
alert(typeof new String('Hello World'));
alert(typeof new Number(5));
will alert "object".
Check if the value is a string literal or String object:
function isString(o) {
return typeof o == "string" || (typeof o == "object" && o.constructor === String);
}
Unit test:
function assertTrue(value, message) {
if (!value) {
alert("Assertion error: " + message);
}
}
function assertFalse(value, message)
{
assertTrue(!value, message);
}
assertTrue(isString("string literal"), "number literal");
assertTrue(isString(new String("String object")), "String object");
assertFalse(isString(1), "number literal");
assertFalse(isString(true), "boolean literal");
assertFalse(isString({}), "object");
Checking for a number is similar:
function isNumber(o) {
return typeof o == "number" || (typeof o == "object" && o.constructor === Number);
}
Simple and thorough:
function isNumber(x) {
return parseFloat(x) == x
};
Test cases:
console.log('***TRUE CASES***');
console.log(isNumber(0));
console.log(isNumber(-1));
console.log(isNumber(-500));
console.log(isNumber(15000));
console.log(isNumber(0.35));
console.log(isNumber(-10.35));
console.log(isNumber(2.534e25));
console.log(isNumber('2.534e25'));
console.log(isNumber('52334'));
console.log(isNumber('-234'));
console.log(isNumber(Infinity));
console.log(isNumber(-Infinity));
console.log(isNumber('Infinity'));
console.log(isNumber('-Infinity'));
console.log('***FALSE CASES***');
console.log(isNumber(NaN));
console.log(isNumber({}));
console.log(isNumber([]));
console.log(isNumber(''));
console.log(isNumber('one'));
console.log(isNumber(true));
console.log(isNumber(false));
console.log(isNumber());
console.log(isNumber(undefined));
console.log(isNumber(null));
console.log(isNumber('-234aa'));
Can you just divide it by 1?
I assume the issue would be a string input like: "123ABG"
var Check = "123ABG"
if(Check == Check / 1)
{
alert("This IS a number \n")
}
else
{
alert("This is NOT a number \n")
}
Just a way I did it recently.
Jsut an FYI, if you're using jQuery you have
$.isNumeric()
to handle this. More details on http://api.jquery.com/jQuery.isNumeric/
You can check the type of variable by using typeof operator:
typeof variable
The code below returns true for numbers and false for anything else:
!isNaN(+variable);
XOR operation can be used to detect number or string. number ^ 0 will always give the same number as output and string ^ 0 will give 0 as output.
Example:
1) 2 ^ 0 = 2
2) '2' ^ 0 = 2
3) 'Str' ^ 0 = 0
What do you thing about this one?
const numberOrString='10'
const isNumber = !isNaN(numberOrString*1)
I know which way I'll be using...
function isNumber(n) { return !isNaN(parseFloat(n)) && !isNaN(n - 0) }
function isNumberRE(n) { return /^-?[\d.]+(?:e-?\d+)?$/.test(n); }
function test(fn, timerLabel) {
console.time(timerLabel)
for (i = 0; i < 1000000; i++) {
const num = Math.random() * 100
const isNum = fn(num)
}
console.timeEnd(timerLabel)
}
test(isNumber, "Normal way")
test(isNumberRE, "RegEx way")
Normal way: 25.103271484375 ms
RegEx way: 334.791015625 ms