JavaScript Displaying the Content

Why does not .map work when I use curly braces in the call back?

When I use curly braces in the callback function within the .map method no list is shown. When these are removed however, it seems to be working. How come?

This does'nt work, function generateOptions(data) { const options = data.map( item => { <option value='${item}'>'${item}'</option> }) select.innerHTML = options; }

This does. function generateOptions(data) { const options = data.map( item => <option value='${item}'>'${item}'</option> ) select.innerHTML = options; }

2 Answers

Blake Larson
Blake Larson
12,876 Points

When the only statement in an arrow function is return, we can remove return and remove the surrounding curly brackets. If you have the curly braces you need to have a return.

Zaid Khan
Zaid Khan
12,743 Points

Thank you Blake for this. Should be marked as the best answer.