0

I am fairly new to Javascript and one question dawned on me... If I ever wanted to get the string of a Javascript object key without getting the field at the same time, how would this be done? I tried searching the web, but it seems like I am using the wrong terminology as I didn't find anything about my question after the first 20 or so links. So how would this be possible? I am learning the in and outs of the language and I am genuinely curious, any help is appreciated.

  • 1
    A `for-in` loop enumerates the enumerable keys of a JavaScript object (well, the ones that are strings; property keys can be strings or Symbols and `for-in` only enumerates the strings). You can also use `Object.keys` to get an array of all of the object's own enumerable string property names, or `Object.getOwnPropertyNames` to get an array of *all* of its own string property names (including non-enumerable ones), or `Object.getOwnPropertySymbols` to get all of its own property Symbol keys. If you want inherited ones, you have to loop over its prototype chain via `Object.getPrototyepOf`. – T.J. Crowder Jan 26 '21 at 07:35
  • 1
    (If you're just learning JavaScript, some of the above may be hard to follow, but you'll learn it as you go. Briefly: Object A can inherit from Object B, in which case Object B is Object A's prototype. So looking at Object A, a property can be an "own" property [it's on Object A] or an inherited one [it's on Object B]. Since Object B may also have a prototype, objects have a "prototype chain" -- Object A to Object B to whatever prototype Object B has, etc.) – T.J. Crowder Jan 26 '21 at 07:38

0 Answers0