javascriptpropertieshasownproperty

Is there a need to use hasOwnProperty with Object.keys?


if I iterate over an object, I need to check whether it is not a prototype object I loop over. (with hasOwnProperty)

If I collect the keys with Object.keys, I always just get the "real keys" back. Is this correct?


Solution

  • Yes, Object.keys() gives you the keys of the properties directly on the object. So you could use that.

    Keep in mind that Object.keys() may not be supported in every one of your target browsers if you're doing front-end development.