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.

Read Also:  PHP Magento Configure Product Attribute sets

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

Read Also:  $digest() Function using Angular Example

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.

Read Also:  How to Join Two Tables in Laravel 6?

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Editing .docx files and update docx files using PHP
  2. Read Also:  how to add a visitor counter code javascript HTML?
  3. Read Also:  Generate Secure Random Strings using Laravel
  4. Read Also:  Stripe API subscriptions with plan coupon and discounts using PHP
  5. Read Also:  how to export div to pdf using jquery?
  6. Read Also:  Angular 6 Services Tutorial with Examples
  7. Read Also:  Codeigniter recaptcha form validation - Google recaptcha
  8. Read Also:  Angular 6 Services Tutorial with Examples
  9. Read Also:  Editing .docx files and update docx files using PHP
  10. Read Also:  PHP Magento Configure Product Attribute sets
CLOSEX