advertise on nct forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How To Check If Javascript Object Is Empty
With JavaScript, it can be difficult to check whether an object is empty.

With Arrays, you can easily check with
myArray.length , but on the other hand, objects do not work that way.
The best way to check if an object is empty is by using a utility function like the one below.

function isEmpty (obj ) {
for(var key in obj ) {
if(obj .hasOwnProperty (key ))
return false;
return true;

So if you have an empty object, you can check whether it is empty by using the above function.

var myObj = {}; // Empty Object
if(isEmpty (myObj )) {
// Object is empty (Would return true in this example)
} else {
// Object is NOT empty

Alternatively, you can write the isEmpty function on the Object prototype.

Object.prototype .isEmpty = function() {
for(var key in this ) {
if(this .hasOwnProperty (key ))
return false;
return true;

Then you can easily check if the object is empty like so.

var myObj = {
myKey: "Some Value"
if(myObj .isEmpty ()) {
// Object is empty
} else {
// Object is NOT empty (would return false in this example)

Extending the object prototype is not the best thing to do as it can cause some browser issues and other issues with certain frameworks (it's also not always reliable in certain environments). The example I gave is pretty much framework agnostic although.

This is one of those utility functions that will always be useful, especially if you deal with a lot of objects on a daily basis (like I do).
It takes so long to learn simplicity.
[-] The following 1 user Likes Dealwap's post:
  • Horlahcoded

Possibly Related Threads...
Thread Author Replies Views Last Post
  SHARED: Introducing Hbot.js Javascript Ai Library Horlahcoded 3 147 11-08-2017, 10:13 AM
Last Post: Horlahcoded
  SHARED: Introducing Hrouter.js Javascript Routing Library Horlahcoded 7 112 10-16-2017, 10:13 PM
Last Post: iamdannyfc
  SHARED: Introducing Hkwest.js Javascript Ajax Library Horlahcoded 0 55 10-15-2017, 09:46 AM
Last Post: Horlahcoded
  HELP: Javascript Vs Jquery. beme 10 316 08-21-2017, 07:20 AM
Last Post: beme
  HELP: Why Is This Code Not Working. Webmasters Check In! Thepreditor 6 179 08-16-2017, 09:26 PM
Last Post: jonath45
  Test Your Javascript Knowledge Dealwap 10 595 08-16-2017, 07:41 PM
Last Post: Thepreditor
  How Can I Submit A Form Using Javascript iamdannyfc 11 263 06-22-2017, 10:13 PM
Last Post: bfaschat
  TUTORIAL: Take A Selfie With Javascript SamBlingxZ 0 94 06-05-2017, 11:41 PM
Last Post: SamBlingxZ
  3 Types Of Javascript Animations That I So Much Admire! Laive 5 230 05-20-2017, 09:40 AM
Last Post: Laive
  HELP: How Can I Create A Javascript Ads Rotator For My Site Eddykul 5 234 03-30-2017, 09:51 AM
Last Post: Harsh

Users browsing this thread: 1 Guest(s)

About Nigeria's Creative Talents

Nigeria's Creative Talents is an online community where every creative Nigerians can share ideas with themselves.

For any more information, please use our contact form.

              User Links