What's the difference between an object and an instance?

Hi. I’m not sure what the difference between an instance and an object is. Can someone please explain?
Thanks

There might be different opinions on this, but I think object tends to refer to the idea or concept of a group of properties and methods bundled together. An instance of an object is when you use the constructor of an object to actually make the object.

As you probably know, when you define an object in JS you are making a blueprint. Running your code with an object definition won’t generate that object. You’ll have to create an instance of that object if you want to use it. Is this making sense?

From MDN:

The term instance has a specific technical meaning in class-based languages. In these languages, an instance is an individual instantiation of a class and is fundamentally different from a class. In JavaScript, “instance” does not have this technical meaning because JavaScript does not have this difference between classes and instances. However, in talking about JavaScript, “instance” can be used informally to mean an object created using a particular constructor function

Link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model#:~:text=In%20JavaScript%2C%20"instance"%20does,using%20a%20particular%20constructor%20function.

1 Like

Thank you for your reply. This kind of makes sense to me. Please correct me if I understand this incorrectly.
log.console(‘Hello’.length());
Is hello the object, .length the constructor and the instance is the value that comes out in the console?
This is all quite new to me.

First, you made a typo. It should be console.log('Hello'.length)

In this example, length is a property of the String object. As you learn more about JavaScript you’ll find out that JS treats pretty much everything as an object. So ‘Hello’ is a String object. When the example uses 'Hello'.length' it is using the property length of the object, which is the string ‘Hello’.

Objects have properties and methods. In JavaScript length is a property. If it were a method, it would be accessed with 'Hello'.length(), but we don’t use () because it is not a method

2 Likes

Whoops. My bad. That makes perfect sense now. Thanks for the help!

1 Like