what is the syntax of
then((response) => response.json())
in javascript?
I have googled a lot, but not find the explanation of => .
what is the syntax of
then((response) => response.json())
in javascript?
I have googled a lot, but not find the explanation of => .
These are known as Arrow function. An example:
// ES5
var selected = allJobs.filter(function (job) {
return job.isSelected();
});
// ES6
var selected = allJobs.filter(job => job.isSelected());
You can find more detailed explanation at ES6 In Depth: Arrow functions.
Syntax:
If you are passing one paramter then it is like
var x = i=> i;
// which is equivalent to:
var x= function(i) {
return i;
};
Also note that there is no explicit return statement in your arrow function then too it is going to return the argument which is passed in it.
Now if you are passing two parameters something like this:
var x= (i1, i2) => i1 + i2;
// which is equivalent to:
var x= function(i1, i2) {
return i1 + i2;
};
In this case you need to pass the two arguments inside the paranthesis.
If your function is not having any argument then you need to put the empty braces like this:
var x = () => 1 + 2;
// which is equivalent to:
var x = function() {
return 1 + 2;
};