The every
method traverses the array and tests every element of the array.
The Syntax section of the documentation for every
shows the various ways this method can be called. In the expression mentioned by you, the syntax is of an arrow function with a single parameter. The syntax is of the form,
every((element) => { /* … */ } )
order.every(item => inventory[item[0]] >= item[1]);
Since item
is the only parameter being provided to the arrow function, so each element of the array will be assigned to item
and then the element will be tested.
Specifically for the example under discussion:
-
In the first iteration,
item
will be["sunglasses", 1]
, soitem[0]
will be"sunglasses"
anditem[1]
will be1
-
In the second iteration,
item
will be["bags", 2]
, soitem[0]
will be"bags"
anditem[1]
will be2
order
is an array of arrays (i.e. of the form [ [], [], ... ]
In order[0][1]
, the [0]
notation targets the first array (i.e the whole array ["sunglasses", 1]
). and the [1]
notation targets the second element of that array (i.e. the integer 1
).
But, in the every
method, the current element being processed will automatically be assigned to the parameter. In our case, the elements of our order
array are arrays, so each array will be automatically assigned to item
. We don’t have to target the arrays ourselves. That work is done for us.