deeveeABC deeveeABC - 3 months ago 13
Node.js Question

What does => mean in node js

I am learning node js, and came across '

=>
' several times, however struggle to understand what this means.

Here is an example:

app.post('/add-item', (req, res) => {
// TODO: add an item to be posted
});


Do we actually need this in the above example? A simple explanation would be helpful. Thanks

Answer

It's nothing node-exclusive, it's an ES6 Arrow function expression

app.post('/add-item', (req, res) => {
  // TODO: add an item to be posted
});

basically means:

app.post('/add-item', function(req, res) {
  // TODO: add an item to be posted
});

The main difference between these two examples is that the first one lexically binds the this value.