-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

5
  • 1
    keyclick would be an array, and in is basically doing a pseudo-code equivalent of if (keyclick[40] exists). Commented Aug 3, 2016 at 19:17
  • in is used to iteration through properties of an object. Commented Aug 3, 2016 at 19:17
  • 1
    in operator says it all. First match goggleing js in Commented Aug 3, 2016 at 19:19
  • 1
    If you think in and == are interchangeable here, you don't understand what the function is doing at all. Commented Aug 3, 2016 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 falses and then toggling the particular value, instead of adding and deleting properties, and checking them with in. Commented Aug 3, 2016 at 19:39

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.