Javascript array key exists / Javascript hasOwnProperty

Description

In javascript if you want to check if there a key that exists in a javascript array or a javascript object, you need to use hasOwnProperty() method of javascript.

Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike thein operator, this method does not check down the object’s prototype chain.

Examples

Using hasOwnProperty to test for a property’s existence

The following example determines whether the o object contains a property named prop:

o = new Object();
o.prop = 'exists';

function changeO() {
  o.newprop = o.prop;
  delete o.prop;
}

o.hasOwnProperty('prop');   // returns true
changeO();
o.hasOwnProperty('prop');   // returns false
previous article

One Comments

  • John May 16, 2016 Reply

    I appreciate, cause I discovered just what I used to be looking for. You have ended my four day long hunt! God Bless you man. Have a nice day. Bye John

Leave a Reply