-1

How can I check if variable in java script is type of a particular object? What will be the result of this

var myvalue = "200"+50+44;
Pramod Ravikant
  • 1,039
  • 2
  • 11
  • 28
  • possible duplicate of [Check whether variable is number or string in javascript](http://stackoverflow.com/questions/1303646/check-whether-variable-is-number-or-string-in-javascript) – Ja͢ck Aug 30 '13 at 07:26

2 Answers2

2

1) The typeof operator returns a string indicating the type of the unevaluated operand.

2) The result will be 2005044

I think you're trying like this

parseInt("200", 10)+50+44 // returns 294

Check parseInt(string, radix) for more information.

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • It's easy to get the wrong base with `parseInt` so it's advisable to define base as well, ie: `parseInt("200", 10)` (here base-10). –  Aug 30 '13 at 07:37
  • @Ken-AbdiasSoftware Yes, you're right because there is chance of having **prefixed '0'(which is considered as octal)**. I missed it, I have updated it. Thank you for your pointers. – Praveen Aug 30 '13 at 07:41
0

The type can be checked with the typeof operator.

typeof myvalue === "number"

The possible types are "number", "string", "object", "undefined". This has a few problems though.

  • typeof someArray === "object"
  • typeof null === "object"

The better way is compare constructors.

  • someArray.constructor === Array
  • someNumber.constructor === Number

You do however need to check if it's null or undefined, because neither have a constructor property.

  • someThing != null && someThing.constructor === SomeConstructor
Brigand
  • 84,529
  • 20
  • 165
  • 173