The following js script receives the variable "datum" from an external source and evaluates what type of data it is. Please, PLEASE tell me there's a more elegant way to write that particular code:

// declare global variable

var datumEval


// is-datum-a-string function

var stringCheck = new function()    {
    if (isNaN(datum)    {
        datumEval = "string";
    }
    else    {
        return;
    }
};


// is-datum-a-bool function

var boolCheck = new function()  {
    if (datum === true || datum === false)  {
        datumEval = "bool";
    }
    else    {
        return;
    }
};


// is-datum-negative function

var negCheck = new function()   {
    if (abs(datum) === datum)   {
        datumEval = "positive";
    }
    else    {
        datumEval = "negative";
    }
};


// is-datum-integer-or-float function

var floatCheck = new function() {
    if (Math.floor(datum) === datum)    {
        datumEval += " integer";
    }
    else    {
        datumEval += " float";
    }
};


// main code block

if (stringCheck(datum)) {
    return datumEval;
}
else if (boolCheck(datum))  {
    return datumEval;
}
else    {
    negCheck(datum);
    floatCheck(datum);
    return datumEval;
}

1)The isNaN() function doesn't check for string but rather for not-a-number. Anything that is not a number is not equal to String.
2)Your boolean check is not valid. In JavaScript, it is tricky when it comes to boolean. Have you ever tried 0==false or 1==true? Both will return true. That's one of the trickiest part of JavaScript.
3)Your negative/positive check is also invalid. There is no abs() function but there is Math.abs() function. You should check whether the datum you are checking is a number before you attempt to check for negative/positive.
4)This integer/float checking could also be invalid. You need to check whether the datum is a number. Also, it isn't that useful to check whether a variable is a float or int in JavaScript. The reason is that JavaScript is a loosy data type. In other words, a variable data type can be changed at anytime in the script without raising an error. There isn't really a primitive datatype (such as int, long, float, double).

If you really want to check data type/class, you should use isNaN(), typeof(), or instanceof() instead. Yet, I still see no need of type checking that much because it is against the idea of the language.

Member Avatar for diafol

As mentioned:
typeOf() for data types IMO.

Why is checking for data types against the idea of the language?

Thanks for your responses, btw.

Member Avatar for diafol

The hard work is done for you with functions like typeOf() - why do you need to re-invent the wheel? You can just run the function once and use conditionals to branch off your code as opposed to making multiple complicated tests. The RollYourOwn tests will probably not work properly (as pointed out).

You're absolutely right, Diafol. Thanks. Something like this?

var datumType = typeOf(datum);
switch { etc}
Member Avatar for diafol

sounds OK to me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.