If you are programming in Javascript then you might need to test a variable for object or null at various levels. Some like to rewrite the code while others like to create a generic function or a php like function to make their life easy. So this is going to be a small snippet of code but is a life saver at times.
<script type='text/javascript'> function is_object(obj) { //you may use either of the following 2 lines return (typeof(obj)!='object')?false:true; //original post's function return (typeof(obj)==’object’) //suggested by HB in the comment } //usage examples var a=new String('Digimantra'); //a is object of class String alert(is_object(a)); //it will return true as a is string object here var a='realin'; // a variable alert(is_object(a)); //it will return false, since a is of the type string now </script>
So as you can see we have used the typeof operator to get if the passed param is an object or no. Place this function in your common JS file and enjoy the function is_object();
Stay Digified!!
Sachin Khosla





The backwards ternary operator is unnecessary, if your expression returns true the ternary returns false, and vice versa. Why not just:
return (typeof(obj)==’object’)
That’s already boolean, and the actual answer you’re checking for.
that was quick and and that very quickly updated the code.
Thanks for sharing info