Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading