---
title: "jQuery.typeof and jQuery.isEmpty Utilities"
author: "yaofeng911"
date: "2009-05-15T15:15:17Z"
tags: ["JavaScript", "jQuery"]
slug: "jquery-typeof-and-jquery-isempty-utilities"
draft: false
lastmod: "2009-05-15T15:15:17Z"
id: 387458
url: "https://yaofeng911.iteye.com/blog/387458"
---

One of the best aspects of jQuery is its light weight. I have really taken advantage of some of its built in Utilities such as jQuery.extend() and jQuery.map(). I am definitely against bloated software, but I find myself writing these functions time and again. They are pretty utilitarian and not too huge. Just a suggestion (code borrowed from Crockford's Remedial [JavaScript](http://yaofeng911.iteye.com/wiki/JavaScript))

 

1\. A better typeof function that actually returns 'array' for an array instead of 'object':

```
function typeOf(value) { 

var s = typeof value; if (s === 'object') { 

if (value) { 

if (typeof value.length === 'number' && 

!(value.propertyIsEnumerable('length')) && typeof value.splice === 'function') { 

s = 'array'; 

} 

} else { 

s = 'null'; 

} 

} return s; 

} 

```

 

2\. A simple isEmpty function that returns a boolean:

```
function isEmpty(o) { 

var i, v; if (typeOf(o) === 'object') { 

for (i in o) { 

v = o[i]; if (v !== undefined && typeOf(v) !== 'function') { 

return false; 

} 

} 

} return true; 

} 
```