- Home /
function that will return the name of the data type of a variable?
Is there some function I can use that will return the name of the data type of a variable? I need to know this so I can call a function out of two functions I made that use different datatypes. I hope that does not sound too confusing. Thanks for the help.
typeof(yourVariable)
efit: i only read title. Yeah you dont need to know variable type for this. Language will know which function to call based on variable you enter.
Answer by Mr.Z · Jan 04, 2013 at 06:57 PM
What alexfeature said is correct and thats deffo the way you should go about it, but if you really want to get the datatype for some strange reason you can do it like this:
function is(type, obj) {
var clas = Object.prototype.toString.call(obj).slice(8, -1);
return obj !== undefined && obj !== null && clas === type;
}
//basic usage
is('String', 'test'); // true
is('Array', true); // false
//Or compare it against any type you want
Answer by alexfeature · Jan 04, 2013 at 05:41 PM
This is called overloading and is a feature of many programming languages.
For example in c# you can do this
void FooMethod (string myString)
{
// ....
}
void FooMethod (int myInt)
{
//....
}
When you call either of these two methods the compiler will deduce wich method you wanted to call based on the type of the parameter you pass.
You dont need to do any checks yourself.
This is what I actually used but the next answer was what I was looking for and I actually am using it too now. thanks though.
Your answer
Follow this Question
Related Questions
GUI calling function with return value. I dont know why this doesnt work. 0 Answers
Ienumerator return string 0 Answers
Have a function wait unitl a boolean is false 1 Answer
Return a custom class from function 1 Answer
code path returning error 2 Answers