hasproperty in javascript – How to Check If an Object Has a Property in JavaScript?

In this best post for hasproperty in javascript, you’ll read all about the 3 methods(hasOwnProperty() method, in operator, Comparing with undefined) to check if an object has a characteristic of an object :

  • Compare with typeof and undefined.
  • Use hasOwnProperty method.
  • Use in keyword.

Comparison with typeof

Check if the type of a characteristic of an object is undefined, is one of the most common practices when client side or full stack developers check if an object has a characteristic of an object. Also read: Javascript Check if Object is empty

typeof function returns a datatype string with the name of the type of the javascript variable as a first arguments (Boolean, Object, undefined etc). So, you’ll required only to compare the string value of the js characteristic of an object that you want with “undefined”.

var allProductObj = {
    litanswers: "mobile"
};

if("undefined" === typeof(allProductObj["litanswers"])){
    // The characteristic of an object DOESN'T exists
}else{
    // The characteristic of an object exists
}

// Or ...

if(typeof(allProductObj.litanswers) === "undefined"){
    // The characteristic of an object DOESN'T exists
}else{
    // The characteristic of an object exists
}

// Or ...

if(typeof(allProductObj.litanswers) !== "undefined"){
    // The characteristic of an object exists
}else{
    // The characteristic of an object DOESN'T exists
}

IMP Note: If the data object has the characteristic of an object with value as undefined, typeof is not recommendable. To prevent doubt, use hasproperty in javascript instead if you use undefined instead of null in your js objects.

hasOwnProperty

A simple Javascript object has commonly the hasOwnProperty pure method. as well as The hasOwnProperty method data returns a boolean value like as a true or false indicating whether the object has the particularized characteristic of an object as first parameter.

here simple example to Unlike the in operator, also you know this ways does not check down the object’s prototype chain.

var allProductObj = {
   litanswers: "This is my litanswers string"
};

if(allProductObj.hasOwnProperty("litanswers")){
   // allProductObj has the litanswers characteristic of an object
}else{
   // allProductObj doesn't has litanswers characteristic of an object
}

// return data value False
var hasPropertylitanswers = allProductObj.hasOwnProperty("pakainfo");

// Use js based pure hasOwnProperty in data arrays too using the index
["litanswers"].hasOwnProperty(0); // return data value true
// But no the value
["litanswers"].hasOwnProperty("litanswers"); // return data value false

Example

const product = {
  name: 'Mobile'
};

product.hasOwnProperty('name');     // => true
product.hasOwnProperty('slugName'); // => false

const product = {
  name: 'Mobile'
};

product.toString; // => function() {...}

product.hasOwnProperty('toString'); // => false

in operator

The JavaScript in operator returns boolean value like as a true if the particularized characteristic of an object is in the particularized object. Also know that you can use in either in object as well as arrays.

var allProductObj = {
   litanswers:"litanswers message"
};

if("litanswers" in allProductObj){
  // litanswers characteristic of an object exists
}else{
  // litanswers characteristic of an object exists
}

// return data value False
var isInObject = ("trello" in allProductObj);

// js Use in in arrays too using the main index
(0 in ["litanswers"]); // return data value true
// But not the value
("litanswers" in ["litanswers"]); // return data value false

Example

const product = {
  name: 'Mobile'
};

'name' in product;     // => true
'slugName' in product; // => false

const product = {
  name: 'Mobile'
};

product.toString; // => function() {...}

'toString' in product;              // => true
product.hasOwnProperty('toString'); // => false

Comparing with undefined

Example

const product = {
  name: 'Mobile'
};

product.name;     // => 'Mobile'
product.slugName; // => undefined

const product = {
  name: 'Mobile'
};

product.name !== undefined;     // => true
product.slugName !== undefined; // => false

Which method is faster?

If i really want to know which is faster and easy to use, you’ll notice the difference only if you work with an extense loop as well as lots of the objects. Check if Object is empty in VueJS

According to the demo testing, the comparison with typeof seems to be pretty faster compared with hasOwnProperty and in.

The javascripts code execution of the last examples 1000000 (1M) times, displays that the in and hasOwnProperty methods take approximately all around twice than the time needed by the comparison with typeof and undefined.

I hope you get an idea about hasproperty in javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP If Else Switch Case Conditional
  2.   ng-focus event Directive using Angular Example
  3.   jQuery ToggleClass to Switch Between Classes
  4.   laravel Check Request is Ajax Example
  5.   Laravel Create Table using Migration and model
  6.   jQuery Modal Popup Window Dialog Box
  7.   Create Dynamic Image Slider using PHP with jQuery
  8.   PHP Image Resize Script Example
  9.   DataTable Custom Filter Server Side Examples
  10.   jquery radio button checked
Tags:, ,