Javascript array key exists / Javascript hasOwnProperty

Share Button

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
Share Button
  • John

    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

  • Johnc336

    I have read several good stuff here. Certainly worth bookmarking for revisiting. I surprise how much effort you put to make such a magnificent informative web site. cebckbkebkde