Javascript: use of => in array operations (not an arrow function)

in

https://www.codecademy.com/courses/asynchronous-javascript/lessons/promises/exercises/onfulfilled-onrejected

there is one operator or operation that i cannot figure out. here is the line in library.js line 10 :

let inStock = order.every(item => inventory[item[0]] >= item[1]);

what is the purpose of the => after item? i have searched long and hard but i cannot find an explanation for it. i presume it is something to do with arrays (php uses something a bit like this), but i would really like to know for sure how to use it.
thx, julian

That tells the interpreter this is a function expression, akin to,

function (item) {

}

The concise body arrow function has relaxed syntax for cases like this. The fat arrow separates parameter from body; parens are not needed on single param; braces are not needed; return is implicit.

Note: Not to be confused with PHP associative arrays. Different language, different symbol usage.

PHP

Array(a => 'value a', b => 'value b')

JS

{a: 'value a', b: 'value b'}
1 Like

i realised that the key is in the .every method, which i looked up:
Array.prototype.every() - JavaScript | MDN
and one section shows this:

Arrow functions provide a shorter syntax for the same test.
[12, 5, 8, 130, 44].every(x => x >= 10); // false
[12, 54, 18, 130, 44].every(x => x >= 10); // true
in other words, => is an arrow function and now it makes sense + i remember it from the intro JS course.

many thanks mtf for the speedy reply. i was confused by the lack of curly braces and the way => works in PHP (as you point out and illustrate).

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.