missymae
missymae
KPCKevin Powell - Community
Created by snxxwyy on 9/28/2024 in #front-end
functions with return
when writing a function, the instructions inside are done in order; and yes, the return statement is what the function will send back when it completes, the return value. It's optional. in JS you can write functions in multiple ways. Here you also use an arrow function, which automatically returns a value - unless it's an {object} in braces, in which case it needs the
return
return
. So you can remove the braces and return from the map and have the same result with
function getAgain(string){
return string.map(string => string.length)
}
function getAgain(string){
return string.map(string => string.length)
}
or you can rewrite the whole works as a const and use arrow functions to avoid writing any returns and have something like this
const getStringSizes= (strings) => strings.map(string => string.length);
const getStringSizes= (strings) => strings.map(string => string.length);
The best way to learn the patterns is just use them and mess around, see what works and what breaks.
10 replies