tejasree vangapalli
tejasree vangapalli

Reputation: 89

how does console.log work with functions without return value

Even though assignEvent function just assigns and does not return any value. I am still seeing value being printed in the below code. Why des it print 19.99 in below code?

let event = {
  name: "hot dog and burger sunday",
  financials: {
    baseCost: "19.99",
    discountsAvailable: false,
    maxCost: '29.99'
  },
  subscribers: [
    // lots of subscribers here
  ]
}

let eventPrice;

const assignEvent = ({financials: {baseCost: price }}) => eventPrice = price 

console.log(assignEvent(event));

Upvotes: 0

Views: 124

Answers (2)

Eneias Silva
Eneias Silva

Reputation: 214

Yes, it does return a value. When you use an arrow function without brackets, which can be done with one line functions, this line is also used as return statement. Since it is an assignment, it returns the value assigned. eventPrice = price is your return statement.

Upvotes: 1

ray
ray

Reputation: 27275

Arrow functions without curly braces around the function body implicitly return the result of the expression. In your case it returns the result of the assignment, which is the value of eventPrice.

Upvotes: 3

Related Questions