0

I'm just learning node.js so Im not sure if this a node.js question or a javascript question but Im not sure what this syntax is/is saying. I don't believe it's being to compare less than or greater than values as I've seen it used like so in node.js apps

router.use('/intro', (req, res) => {

});

What is the "=>" saying / what is it's significance?

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
garrettmac
  • 8,417
  • 3
  • 41
  • 60

1 Answers1

2

This is a javascript question, it is a new way to write functions...

This is simply es6 arrow function syntax.

ES5

var a = array.map(function(s){ return s.length });

ES6

var b = array.map( s => s.length );

Your code in ES5:

router.use('/intro', function(req, res) {

});

Note: Differences with Lexical this

Until arrow functions, every new function defined its own this value (a new object in case of a constructor, undefined in strict mode function calls, the context object if the function is called as an "object method", etc.). This proved to be annoying with an object-oriented style of programming.

See these docs for more info

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

omarjmh
  • 13,632
  • 6
  • 34
  • 42
  • awesome this is exactly what I was looking for – garrettmac Apr 16 '16 at 02:11
  • edited answer, check it out – omarjmh Apr 16 '16 at 02:12
  • this is not a new way of writing functions, it is an arrow function, which isn not the same as a normal function – Yerken Apr 16 '16 at 02:14
  • @JordanHendrix your answer is misleading, arrow function is a new feature introduced for various reasons (the main ones are: 1. bind `this` 2. concise syntax ). Which means, arrow function are not here to substitute normal functions. You cannot use arrow function with generators, and in some other scenarious – Yerken Apr 16 '16 at 02:21