-4

Hi i'm following a tutorial learning to use javascript.

function move(keyclick) {
if(40 in keyclick) {}
playerMove.x++;
render(); }

What does the 'in' word mean? I understand what the function is doing, but why not just use == ?

Thanks

Night.owl
  • 121
  • 1
  • 2
  • 13
  • 1
    keyclick would be an array, and `in` is basically doing a pseudo-code equivalent of `if (keyclick[40] exists)`. – Marc B Aug 03 '16 at 19:17
  • in is used to iteration through properties of an object. – Yasin Yaqoobi Aug 03 '16 at 19:17
  • 1
    [in operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) says it all. First match goggleing `js in` – DavidDomain Aug 03 '16 at 19:19
  • 1
    If you think [`in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) and `==` are interchangeable here, you don't understand what the function is doing at all. –  Aug 03 '16 at 19:20
  • Off Topic: @Night.owl, `40 in keyclick` implies that the code managing keyclick is adding and deleting properties, wich is not good. Since the key is numeric I assume that `keyclick` is an Array? It would be be better to initialize this Array with 256 `false`s and then toggling the particular value, instead of adding and deleting properties, and checking them with `in`. – Thomas Aug 03 '16 at 19:39

2 Answers2

1

The in operator is true if the string on the LHS is the name of a property that exists on the object on the RHS.

== tests if a value matches another value, which is entirely different.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

The in operator returns true if the specified property is in the specified object (cited from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in).

4lackof
  • 1,250
  • 14
  • 33