6

I have a variable var number="1234", though the number is a numeric value but it is between "" so when I check it using typeof or by NaN I got it as a string .

function test()
{
    var number="1234"

if(typeof(number)=="string")
{
    alert("string");
}
if(typeof(number)=="number")
{
    alert("number");
}

}

I got always alert("string"), can you please tell me how can I check if this is a number?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Romi
  • 4,833
  • 28
  • 81
  • 113
  • since you are storing it with "" it will be string only. you need to give var number = 12345 – Gaurav Shah Jul 23 '11 at 10:44
  • 1
    Have a look at the functions defined in this answer - http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric – ipr101 Jul 23 '11 at 10:48

6 Answers6

8

As far I understand your question you are asking for a test to detect if a string represents a numeric value.

A quick test should be

function test() {
   var number="1234"
   return (number==Number(number))?"number":"string"
}

As Number, if called without the new keyword convert the string into a number. If the variable content is untouched (== will cast back the numeric value to a string) you are dealing with a number. It is a string otherwise.

function isNumeric(value) {
   return (value==Number(value))?"number":"string"
}

/* tests evaluating true */
console.log(isNumeric("1234"));  //integer
console.log(isNumeric("1.234")); // float
console.log(isNumeric("12.34e+1")); // scientific notation
console.log(isNumeric(12));     // Integer
console.log(isNumeric(12.7));   // Float
console.log(isNumeric("0x12")); // hex number

/* tests evaluating false */
console.log(isNumeric("1234e"));
console.log(isNumeric("1,234"));
console.log(isNumeric("12.34b+1"));
console.log(isNumeric("x"));
Eineki
  • 14,773
  • 6
  • 50
  • 59
  • Fails where value is empty string or whitespace as both convert to 0 and `0 == ''`. – RobG Jul 23 '11 at 13:18
5

The line

 var number = "1234";

creates a new String object with the value "1234". By putting the value in quotes, you are saying that it a string.

If you want to check if a string only contains numeric digits, you can use regular expressions.

if (number.match(/^-?\d+$/)) {
    alert("It's a whole number!");
} else if (number.match(/^-?\d+*\.\d+$/)) {
    alert("It's a decimal number!");
}

The pattern /^\d+$/ means: at the start (^) of the string, there is an optional minus sign (-?), then a digit (\d), followed by any more digits (+), and then the end of the string ($). The other pattern just looks for a point between the groups of digits.

Jeremy
  • 1
  • 85
  • 340
  • 366
  • 1
    That will work provided the value is only digits, any other value (such as -1 or 1.5) will return false. A robust *isNumber()* function has been posted in response to [Validate numbers in JavaScript - IsNumeric()](http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/1830844#1830844). – RobG Jul 23 '11 at 12:59
2

See parseInt and parseFloat

spender
  • 117,338
  • 33
  • 229
  • 351
  • using parseInt , it converts both "abcd" and "1234" to a integer. which gives me wrong result. i want only "1234" to be converted into inumeric value – Romi Jul 23 '11 at 10:54
  • @Romi, actually the "numeric" value of 'romi' is NaN (not a number). Perhaps you should check if the resultant variable isNaN (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/isNaN) – spender Jul 23 '11 at 21:44
0

because var number="1234" is a string. the double quotes makes it a literal.

if you want a number use it like this

var number = 1234;

Update:

If you are taking the input from a input tag forexample, the dataType will be string, if you want to convert it to a number, you can use the parseInt() function

var number = "1234";

var newNumber = parseInt(number);

alert(typeof newNumber); // will result in string
Ibu
  • 42,752
  • 13
  • 76
  • 103
  • Ibu: Ya absolutely, but what my problem is i ma getting through it a text box from where i am getting "1234" how can i remove "" to make it a number. although when i put alert on the data i got from text box i get it as 1234 but while checking it gives me a string – Romi Jul 23 '11 at 10:48
  • See my update @Romi, using parseInt() for integers or parseFloat() for decimal numbers will help fix this problem – Ibu Jul 23 '11 at 10:50
  • Fails where the input has trailing non-number characters. – RobG Jul 23 '11 at 13:20
0

Convert it to a number then compare it to the original string.

if ( parseFloat(the_string,10) == the_string ) {
    // It is a string containing a number (and only a number)
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Another easy way:

var num_value = +value;
if(value !== '' && !isNaN(num_value)) {
    // the string contains (is) a number
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • empty string converts to zero, so `!isNaN(+(''))` gives false. Also whitespace and *null* convert to zero too, so they will return true (though in this case null is not an expected value). – RobG Jul 23 '11 at 12:51
  • I think the simplest is: `function isNumber(n) { return !isNaN(parseFloat(n)) && isFinite(n); }` posted [here](http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/1830844#1830844). – RobG Jul 23 '11 at 12:56