Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does => mean in node js [duplicate]

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

like image 581
deeveeABC Avatar asked Sep 05 '16 10:09

deeveeABC


2 Answers

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.

like image 171
roberrrt-s Avatar answered Oct 03 '22 02:10

roberrrt-s


This is just a different way of writing an anonymous function:

$(document).ready(() => {
    console.log('Hello I am typescript');
});

is equivalent to JavaScript:

$(document).ready(function(){
    console.log('Hello I am typescript');
});
like image 33
Vishal Gupta Avatar answered Oct 03 '22 02:10

Vishal Gupta