While searching for getters and setters, I found a JavaScript code that I didn't understand

function Car() {
  const fuel = 50
  return {
    fuel
  }
}

const car = Car();
console.log(car);

I happened to see the code above, and I thought the value of car would be 50, but strangely, car became an object. Can you tell me why car is an object? I happened to see it in the link below.
Why use getters and setters in JavaScript?

>Solution :

Notice the curly braces {} when it returns. These mean that it is an object that contains fuel. If you returned just fuel(without {} ) then it would be 50 as you expected.

Leave a Reply