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
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.
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).
inis basically doing a pseudo-code equivalent ofif (keyclick[40] exists).js ininand==are interchangeable here, you don't understand what the function is doing at all.40 in keyclickimplies that the code managing keyclick is adding and deleting properties, wich is not good. Since the key is numeric I assume thatkeyclickis an Array? It would be be better to initialize this Array with 256falses and then toggling the particular value, instead of adding and deleting properties, and checking them within.