3

for example:

greetings = ['hey','hi','hello']

Object.keys(greetings) // ["0", "1", "2"]

BUT

keys(greetings) // [0, 1, 2]
VLAZ
  • 26,331
  • 9
  • 49
  • 67
jayykaa
  • 103
  • 2
  • 10
  • 2
    What is `keys()`? – VLAZ Jul 30 '21 at 18:36
  • @VLAZ it seems to be doing the same thing, i tried it on the console... just a function that gives u keys in an object. – jayykaa Jul 30 '21 at 18:39
  • 1
    Keys will always be a string. Check [this question](https://stackoverflow.com/a/3633390/13464279) – J.F. Jul 30 '21 at 18:39
  • 1
    @jayykaa we prefer to not go by "seems like" but something more actionable. I can give you the reference for `Object.keys` but not for `keys`. If it's non-standard, we probably shouldn't be guessing what it does. – VLAZ Jul 30 '21 at 18:40
  • totally understood, trying to learn here. :) – jayykaa Jul 30 '21 at 18:45
  • 1
    Question I have is why would you want to use Object.keys on an array in the first place – epascarello Jul 30 '21 at 18:47
  • @epascarello im learning javascript. one of the things i was doing was converting an array into an object. and also converting back. just playing around...but on the console..so seems i need to note differences. – jayykaa Jul 30 '21 at 18:49

1 Answers1

1

You are trying the command line API's keys, which isn't actually part of standard JavaScript. It's something Chromium-based browsers inject in DevTools.

As for why it returns different results, not sure. On the other hand, in JavaScript, indexing with a number is identical to indexing with the string version of that number, so it doesn't really matter.

Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31
  • On FF `keys(["a", "b", "c"])` returns `[ "0", "1", "2" ]` – VLAZ Jul 30 '21 at 18:39
  • i dont know enough about this, so if u get votes ill market it as the answer! thanks – jayykaa Jul 30 '21 at 18:40
  • 2
    For me it returns a number array. Since it isn't actually part of any JS specification, it actually doesn't matter how it behaves. Regular JavaScript (in ` – Kelvin Schoofs Jul 30 '21 at 18:40