0

What does => mean in javascript? I see it here

var N = 50

function asyncFunc (cb) {  
  setTimeout(() => cb(Math.random()), 100)
}

function loop (max, results, done) {  
  // Recursion base-case
  if (results.length >= max) return done(results)

  asyncFunc((res) => {
    results.push(res)
    loop(max, results, done)
  })
}

let randomNumbers = []  
loop(N, randomNumbers, function (results) {  
  console.log(results)
})

It appears twice in setTimeout() and asyncFunc().

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
Melissa
  • 1,236
  • 5
  • 12
  • 29

2 Answers2

-1

This is called the 'fat arrow' it will be coming to JavaScript with ES6 but it has also be implemented in languages that transpile to javascript like coffescript. You can read about it more here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Robert Corey
  • 703
  • 7
  • 19
  • FWIW, `=>` is not an *operator*, just like `function` is not an operator. It's simply part of an *arrow function* (no fat, because there is no thin). *"it will be coming to JavaScript with ES6 "* It already came! – Felix Kling Nov 12 '15 at 17:49
-2

It's an "arrow function" - a new (in ES6 aka ES1015) short hand notation for functions that's particularly useful for callbacks.

Alnitak
  • 334,560
  • 70
  • 407
  • 495