/    Sign up×
Community /Pin to ProfileBookmark

Fat-arrow function

This works fine with the .filter() function:

[code]
let tmp = wds.filter(function(w) { return w.length > 4; } );
[/code]

Neither of the following works with the fat-arrow functions

[code]
let tmp = wds.filter( w => w.length > 4; );
let tmp = wds.filter( w => (w.length > 4); );
[/code]

wds is just an array of variable length words (strings).
I’m trying to eliminate the words of less than 5 characters.
Anyone see where I’m going wrong with either of the fat-arrow versions?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumSep 18.2019 — This works for me:
const wds = [
'Hydrogen',
'Helium',
'Lithium',
'Beryllium'
];
let tmp1 = wds.filter(function (w) { return w.length > 6; });
let tmp2 = wds.filter(w => w.length > 6);
let tmp3 = wds.filter(w =&gt; { return w.length &gt; 6; }); <br/>
Here: `let tmp = wds.filter( w =&gt; w.length &gt; 4; ); </C> the semicolon inside the braces is wrong.<br/>
Here: <C>
let tmp = wds.filter( w =&gt; (w.length &gt; 4); ); `
you have to use curly braces for the function body and omit the semicolon inside the outer braces.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/functions
Copy linkTweet thisAlerts:
@JMRKERauthorSep 18.2019 — @Sempervivum#1608890

Thank you. Those extra ';' always bite me in the butt. I guess because they come at the end :)
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...