How is () => ⦠different from () =>
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
I've found a weird issue.
given a filter and an array of objects, I would like to select only those objects that match the filter.
Weirdly, this doesn't work
this.state.articles.filter((article) =>
article.category === filter
)
while this does
this.state.articles.filter((article) => article.category === filter )
I originally thought they would evaluate the same, but it doesn't seem to be the case. Any ideas why?
javascript
add a comment |Â
up vote
7
down vote
favorite
I've found a weird issue.
given a filter and an array of objects, I would like to select only those objects that match the filter.
Weirdly, this doesn't work
this.state.articles.filter((article) =>
article.category === filter
)
while this does
this.state.articles.filter((article) => article.category === filter )
I originally thought they would evaluate the same, but it doesn't seem to be the case. Any ideas why?
javascript
The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
â ibrahim mahrir
35 mins ago
2
(article) => article.category === filter )
is(article) => return article.category === filter )
â epascarello
34 mins ago
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â Gabriele Petrioli
33 mins ago
1
Possible duplicate of When should I use `return` in es6 Arrow Functions?
â Ivar
32 mins ago
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I've found a weird issue.
given a filter and an array of objects, I would like to select only those objects that match the filter.
Weirdly, this doesn't work
this.state.articles.filter((article) =>
article.category === filter
)
while this does
this.state.articles.filter((article) => article.category === filter )
I originally thought they would evaluate the same, but it doesn't seem to be the case. Any ideas why?
javascript
I've found a weird issue.
given a filter and an array of objects, I would like to select only those objects that match the filter.
Weirdly, this doesn't work
this.state.articles.filter((article) =>
article.category === filter
)
while this does
this.state.articles.filter((article) => article.category === filter )
I originally thought they would evaluate the same, but it doesn't seem to be the case. Any ideas why?
javascript
javascript
asked 36 mins ago
mark
440315
440315
The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
â ibrahim mahrir
35 mins ago
2
(article) => article.category === filter )
is(article) => return article.category === filter )
â epascarello
34 mins ago
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â Gabriele Petrioli
33 mins ago
1
Possible duplicate of When should I use `return` in es6 Arrow Functions?
â Ivar
32 mins ago
add a comment |Â
The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
â ibrahim mahrir
35 mins ago
2
(article) => article.category === filter )
is(article) => return article.category === filter )
â epascarello
34 mins ago
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â Gabriele Petrioli
33 mins ago
1
Possible duplicate of When should I use `return` in es6 Arrow Functions?
â Ivar
32 mins ago
The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
â ibrahim mahrir
35 mins ago
The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
â ibrahim mahrir
35 mins ago
2
2
(article) => article.category === filter )
is (article) => return article.category === filter )
â epascarello
34 mins ago
(article) => article.category === filter )
is (article) => return article.category === filter )
â epascarello
34 mins ago
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â Gabriele Petrioli
33 mins ago
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â Gabriele Petrioli
33 mins ago
1
1
Possible duplicate of When should I use `return` in es6 Arrow Functions?
â Ivar
32 mins ago
Possible duplicate of When should I use `return` in es6 Arrow Functions?
â Ivar
32 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
13
down vote
When you open a block in an arrow function, the
return
isn't implied anymore.
You have to write it down :
this.state.articles.filter((article) =>
return article.category === filter
)
2
"implicit return" is the term if anyone wants to Google it.
â Max Baldwin
33 mins ago
This is equivalent tofunction(article) [...]
, in ES5 and older, while(article) => article.category === filter
is equivalent tofunction(article) return [...]
.
â Ismael Miguel
33 mins ago
add a comment |Â
up vote
4
down vote
Javascript ES6 arrow functions work in a particular manner which can best be described via an example:
let multiply1 = (number) => number * 2;
// When we are returning one value we can put this expression on the same line
// this is the same as:
let multiply2 = (number) => return number * 2;
//when we have 1 argument we can omit the parentheses
let multiply3 = number => number * 2;
// When we want to write multiple line we have to put brackets like this:
let multiply4 = (number) =>
console.log('inside arrow function');
return number * 2;
;
console.log(multiply1(2));
console.log(multiply2(2));
console.log(multiply3(2));
console.log(multiply4(2));
When the arrow function is returning an expression it is very convenient to not have to explicitly write the return
statement and the square brackets . This allows for more concise code.
add a comment |Â
up vote
0
down vote
The difference is that when you use () => x
, it really means () => return x
, so just how the statement article.category === filter
on its own dosen't do anything, article.category === filter
dosen't explicitly return anything.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
down vote
When you open a block in an arrow function, the
return
isn't implied anymore.
You have to write it down :
this.state.articles.filter((article) =>
return article.category === filter
)
2
"implicit return" is the term if anyone wants to Google it.
â Max Baldwin
33 mins ago
This is equivalent tofunction(article) [...]
, in ES5 and older, while(article) => article.category === filter
is equivalent tofunction(article) return [...]
.
â Ismael Miguel
33 mins ago
add a comment |Â
up vote
13
down vote
When you open a block in an arrow function, the
return
isn't implied anymore.
You have to write it down :
this.state.articles.filter((article) =>
return article.category === filter
)
2
"implicit return" is the term if anyone wants to Google it.
â Max Baldwin
33 mins ago
This is equivalent tofunction(article) [...]
, in ES5 and older, while(article) => article.category === filter
is equivalent tofunction(article) return [...]
.
â Ismael Miguel
33 mins ago
add a comment |Â
up vote
13
down vote
up vote
13
down vote
When you open a block in an arrow function, the
return
isn't implied anymore.
You have to write it down :
this.state.articles.filter((article) =>
return article.category === filter
)
When you open a block in an arrow function, the
return
isn't implied anymore.
You have to write it down :
this.state.articles.filter((article) =>
return article.category === filter
)
answered 35 mins ago
Zenoo
8,97632043
8,97632043
2
"implicit return" is the term if anyone wants to Google it.
â Max Baldwin
33 mins ago
This is equivalent tofunction(article) [...]
, in ES5 and older, while(article) => article.category === filter
is equivalent tofunction(article) return [...]
.
â Ismael Miguel
33 mins ago
add a comment |Â
2
"implicit return" is the term if anyone wants to Google it.
â Max Baldwin
33 mins ago
This is equivalent tofunction(article) [...]
, in ES5 and older, while(article) => article.category === filter
is equivalent tofunction(article) return [...]
.
â Ismael Miguel
33 mins ago
2
2
"implicit return" is the term if anyone wants to Google it.
â Max Baldwin
33 mins ago
"implicit return" is the term if anyone wants to Google it.
â Max Baldwin
33 mins ago
This is equivalent to
function(article) [...]
, in ES5 and older, while (article) => article.category === filter
is equivalent to function(article) return [...]
.â Ismael Miguel
33 mins ago
This is equivalent to
function(article) [...]
, in ES5 and older, while (article) => article.category === filter
is equivalent to function(article) return [...]
.â Ismael Miguel
33 mins ago
add a comment |Â
up vote
4
down vote
Javascript ES6 arrow functions work in a particular manner which can best be described via an example:
let multiply1 = (number) => number * 2;
// When we are returning one value we can put this expression on the same line
// this is the same as:
let multiply2 = (number) => return number * 2;
//when we have 1 argument we can omit the parentheses
let multiply3 = number => number * 2;
// When we want to write multiple line we have to put brackets like this:
let multiply4 = (number) =>
console.log('inside arrow function');
return number * 2;
;
console.log(multiply1(2));
console.log(multiply2(2));
console.log(multiply3(2));
console.log(multiply4(2));
When the arrow function is returning an expression it is very convenient to not have to explicitly write the return
statement and the square brackets . This allows for more concise code.
add a comment |Â
up vote
4
down vote
Javascript ES6 arrow functions work in a particular manner which can best be described via an example:
let multiply1 = (number) => number * 2;
// When we are returning one value we can put this expression on the same line
// this is the same as:
let multiply2 = (number) => return number * 2;
//when we have 1 argument we can omit the parentheses
let multiply3 = number => number * 2;
// When we want to write multiple line we have to put brackets like this:
let multiply4 = (number) =>
console.log('inside arrow function');
return number * 2;
;
console.log(multiply1(2));
console.log(multiply2(2));
console.log(multiply3(2));
console.log(multiply4(2));
When the arrow function is returning an expression it is very convenient to not have to explicitly write the return
statement and the square brackets . This allows for more concise code.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Javascript ES6 arrow functions work in a particular manner which can best be described via an example:
let multiply1 = (number) => number * 2;
// When we are returning one value we can put this expression on the same line
// this is the same as:
let multiply2 = (number) => return number * 2;
//when we have 1 argument we can omit the parentheses
let multiply3 = number => number * 2;
// When we want to write multiple line we have to put brackets like this:
let multiply4 = (number) =>
console.log('inside arrow function');
return number * 2;
;
console.log(multiply1(2));
console.log(multiply2(2));
console.log(multiply3(2));
console.log(multiply4(2));
When the arrow function is returning an expression it is very convenient to not have to explicitly write the return
statement and the square brackets . This allows for more concise code.
Javascript ES6 arrow functions work in a particular manner which can best be described via an example:
let multiply1 = (number) => number * 2;
// When we are returning one value we can put this expression on the same line
// this is the same as:
let multiply2 = (number) => return number * 2;
//when we have 1 argument we can omit the parentheses
let multiply3 = number => number * 2;
// When we want to write multiple line we have to put brackets like this:
let multiply4 = (number) =>
console.log('inside arrow function');
return number * 2;
;
console.log(multiply1(2));
console.log(multiply2(2));
console.log(multiply3(2));
console.log(multiply4(2));
When the arrow function is returning an expression it is very convenient to not have to explicitly write the return
statement and the square brackets . This allows for more concise code.
let multiply1 = (number) => number * 2;
// When we are returning one value we can put this expression on the same line
// this is the same as:
let multiply2 = (number) => return number * 2;
//when we have 1 argument we can omit the parentheses
let multiply3 = number => number * 2;
// When we want to write multiple line we have to put brackets like this:
let multiply4 = (number) =>
console.log('inside arrow function');
return number * 2;
;
console.log(multiply1(2));
console.log(multiply2(2));
console.log(multiply3(2));
console.log(multiply4(2));
let multiply1 = (number) => number * 2;
// When we are returning one value we can put this expression on the same line
// this is the same as:
let multiply2 = (number) => return number * 2;
//when we have 1 argument we can omit the parentheses
let multiply3 = number => number * 2;
// When we want to write multiple line we have to put brackets like this:
let multiply4 = (number) =>
console.log('inside arrow function');
return number * 2;
;
console.log(multiply1(2));
console.log(multiply2(2));
console.log(multiply3(2));
console.log(multiply4(2));
answered 21 mins ago
Willem van der Veen
2,5292721
2,5292721
add a comment |Â
add a comment |Â
up vote
0
down vote
The difference is that when you use () => x
, it really means () => return x
, so just how the statement article.category === filter
on its own dosen't do anything, article.category === filter
dosen't explicitly return anything.
add a comment |Â
up vote
0
down vote
The difference is that when you use () => x
, it really means () => return x
, so just how the statement article.category === filter
on its own dosen't do anything, article.category === filter
dosen't explicitly return anything.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The difference is that when you use () => x
, it really means () => return x
, so just how the statement article.category === filter
on its own dosen't do anything, article.category === filter
dosen't explicitly return anything.
The difference is that when you use () => x
, it really means () => return x
, so just how the statement article.category === filter
on its own dosen't do anything, article.category === filter
dosen't explicitly return anything.
answered 13 mins ago
DeltaMarine101
186
186
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52334319%2fhow-is-different-from%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
â ibrahim mahrir
35 mins ago
2
(article) => article.category === filter )
is(article) => return article.category === filter )
â epascarello
34 mins ago
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/â¦
â Gabriele Petrioli
33 mins ago
1
Possible duplicate of When should I use `return` in es6 Arrow Functions?
â Ivar
32 mins ago