When we don't use curly brackets on functions?

Hi! This is my very first post. I’m quite a noob. So my issue is that im doing this exercise.


first i tried with a loop and it work out but then I decided to go extra mile and try with the method every().

const isTheDinnerVegan = array => {array.every(food => food.source === 'plant')}

const dinner = [{name: 'hamburger', source: 'meat'}, {name: 'cheese', source: 'dairy'}, {name: 'ketchup', source:'plant'}, {name: 'bun', source: 'plant'}, {name: 'dessert twinkies', source:'unknown'}];

//prints undefined should print false

After peeking into the solution I found out my problem was the curly brackets! Why we don’t use them in the first function?
What’s the purpose of the {}?

Sorry if it sounded stupid the question i feel i’m missing something…

normally we use curly brackets, but JS offers a shorthand, if the body of the function is a single line, we can omit the curly brackets and then the value is “automatically” returned.

so normal scenario:

array.every(food => { return  food.source === 'plant' })


array.every(food => food.source === 'plant') // omitting both {} and return
1 Like

Thank you very much! I asked google and couldn’t find the solution anywhere.

arrow function expression documentation:


the shorthands are listed:

(param1, param2, …, paramN) => expression
// equivalent to: => { return expression; }
// Parentheses are optional when there's only one parameter name:
(singleParam) => { statements }
singleParam => { statements }

both these shorthands can be combined just like you did (food => food.source === 'plant')