+ 2

How to use the In operator

I don’t quite see how to use the « in » operator with javascript. I’ve seen it sometimes with a for loop and also within a if statement. How does it work ? When should we use it ? Thank you

23rd Nov 2018, 10:27 AM
Andrea Vinci
Andrea Vinci - avatar
9 Antworten
+ 5
HonFu : your example works only because there's an index/property '2' in your array... but try this snippet to convince yourself: a = [10, 20, 30]; console.log(20 in a); // false console.log(2 in a); // true ;)
23rd Nov 2018, 5:03 PM
visph
visph - avatar
+ 4
HonFu : not exactly... if you want to check if an element is in an array, you need to use its .includes() method, because the 'in' operator check for object/array keys, not values. Andrea Vinci : 'in' operator in 'for' loops iterate over object/array own properties (keys defined in instance, not in prototype). JS arrays are just special objects where you could define others properties : var myArr = [42,8,256]; myArr.myProp = 'forty-two'; for (var i in myArr) console.log(i+': '+myArr[i]); // output: 0: 42 1: 8 2: 256 myProp: forty-two So, array iteration is more safe to be done with .forEach() method, wich will only iterate over array items, even if you have defined other properties, and using numbers as index values instead of string for keys values: var myArr = [42,8,256]; myArr.myProp = 'forty-two'; myArr.forEach((v,i) => console.log(i+': '+typeof i)); for (var i in myArr) console.log(i+': '+typeof i); // output: 0: number 1: number 2: number 0: string 1: string 2: string myProp: string
23rd Nov 2018, 4:35 PM
visph
visph - avatar
+ 3
HonFu : JS Arrays are 'basically' (but more) an object {'0':'item at index 0','1':'item at index 1'}, but designed to be handled as an array (with specific properties and methods, such as the 'length' property, and with specific behavior, such as update of the length property when adding/removing items or update of keys values when removing/inserting items anywhere else than at end of it).
23rd Nov 2018, 5:27 PM
visph
visph - avatar
+ 2
For example if you want to check if a certain element is an element of an array, you can go like: if (1 in your_array) {... So you don't have to manually write a loop to browse.
23rd Nov 2018, 11:03 AM
HonFu
HonFu - avatar
+ 2
visph ... oO. Crazy ... (says the confused Pythonist) So basically, if you cram stuff into an array, it is interpreted as - in Python - {0: 'x', 1: 'y'}? And if I ask for 2 in a, I get the answer yes, because there's a value with the automatically given name/key 2 (quasi index) in it?
23rd Nov 2018, 5:06 PM
HonFu
HonFu - avatar
+ 1
visph, thank you for pointing this out! I read the JS tutorial on a trip a while ago but obviously forgot a lot, so this came at the perfect moment. ;)
23rd Nov 2018, 5:20 PM
HonFu
HonFu - avatar
0
visph, what do you mean by 'not exactly'? Of course there will always be a case where one tool doesn't work. I gave an example for an array that makes use of 'in'. I just tried the code below to make sure and it seems to work exactly as I said. a = [1, 2, 3]; console.log(2 in a); // true console.log(4 in a); // false
23rd Nov 2018, 4:56 PM
HonFu
HonFu - avatar
0
Andrea Vinci, if you want a simple explanation of what "in" do in javascript, check David Carroll's comment in Morpheus JS Fact Series #054 https://www.sololearn.com/post/46008/?ref=app
25th Nov 2018, 8:54 AM
Gordon
Gordon - avatar