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;
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;
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.
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