Difference between these methods? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Difference between these methods?

Any difference in these three object.keys() for in loop object.getOwropertyNames()

16th Mar 2022, 3:52 AM
Umar Farooq
3 Answers
+ 2
yeah there is one difference i know object.keys() will return an array of all keys which are enumerable ( means countable or numerable ). object.getOwnpropertyNames() will return an array of all the key, does not matter if they are enumerable or not . it will return every property. similarly for in loop will iterate over only those keys which are enumerable. for example var objName = { prop1: { enumerable: true, value: 100 }, prop2: { enumerable: false, value: 9999 }, prop3: 555 // by default all property enumerable value is set to true } then Object.keys(objName) // [ "prop1", "prop3" ] Object.getOwnpropertyName(a) // [ "prop1", "prop2", "prop3" ] for in loop will iterate over "prop1" and "prop3" only not "prop2"
16th Mar 2022, 4:42 AM
NonStop CODING
NonStop CODING - avatar
0
Ty. You mean by default they all are same if we didnt specify whether its enumerable(which i think most people do)
18th Mar 2022, 12:14 AM
Umar Farooq
0
UMAR FAROOQ yes👍
18th Mar 2022, 2:38 AM
NonStop CODING
NonStop CODING - avatar