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
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.
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');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With