-1

I am coming from PHP and Java background. I see the new syntax that uses arrows.

Can someone explain to me what this syntax do and how should I use it?

app.listen(port, (err) => {
    if (err) {
        return console.log('something bad happened', err)
    }

    console.log(`server is listening on ${port}`)
})

I'm specially confused about (err) => { //code } part.

Tal Avissar
  • 9,650
  • 5
  • 40
  • 66
Rajat Saxena
  • 3,720
  • 4
  • 43
  • 60
  • 1
    Can downvoters comment, how can I effectively Google the coding pattern when I don't know the technical name of it (like clojures etc.)? I tried to read up some JS tutorials but did not find anything like this that's why I posted here. – Rajat Saxena May 11 '16 at 18:33
  • 5
    If you didn't ask for external documentation, then it would be a duplicate of [What's the meaning of “=>” (an arrow formed from equals & greater than) in JavaScript?](http://stackoverflow.com/q/24900875/218196). If you don't know the term to look for, sometimes it's helpful to search for the name of the punctuator (as shown in the title of the linked question). Otherwise I agree, searching for syntax is hard. You'd have to look through a reference, such as https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference . – Felix Kling May 11 '16 at 18:36

1 Answers1

1

This is an arrow function. Check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions.

Joe Attardi
  • 4,229
  • 3
  • 36
  • 40