Then our new function will be able to return the correct type of function and array. Of course we can extend our function to detect Date, RegExp. Or we can get fancy by using Sting..prototype.slice to make thing easier.

1234567891011121314

vartoString=Object.prototype.toString,type;functiongetRealType(obj){type=typeofobj;if(type==='object'){// We know we will get something like '[Object Array]', what we want is 'Array'type=toString.call(obj).slice(8,-1);}returntype;}getRealType([1,2,3])==='Array'getRealType(newDate())==='Date'

Conclusion

Although sometime Javascript typeof doesn’t provide the detail information we want, we can make it better by using Objecet.prototype.toString. So when we want to detect number, string etc, typeof is really easy and handy. When it comes to array, function etc, typeof returns object, we can use the method above.