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.

ihttps://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/web-dev-intermediate-javascript-practice/lessons/intermediate-javascript-coding-challenge/exercises/is-the-dinner-vegan

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'}];

console.log(isTheDinnerVegan(dinner))
//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' })

shorthand:

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:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

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')