views:

27

answers:

1

Hi there, I am currently using the isNaN function to check if my variable is a string or an object. I just wondered if this is the wrong way of doing it because it does not seem to be working.

if(isNaN(element))
    element = document.querySelector(element);

At the moment even if element is an object it is still causing isNaN to return true. Any ideas? I am sure I am missing something obvious. Is it that isNaN only works for a string/integer test?

Thanks in advance.

+5  A: 

You can use typeof operator:

var booleanValue = true; 
var numericalValue = 354;
var stringValue = "This is a String";
alert(typeof booleanValue) // displays "boolean"
alert(typeof numericalValue) // displays "number"
alert(typeof stringValue) // displays "string"

Example from this webpage. (Example was slightly modified though).

Here's reference for typeof operator.

Pablo Santa Cruz
Just what I needed, thanks!
Wolfy87