What Is an Object?
Now that you’ve learned how to create a Class, it’s time to understand Objects.
You’ve already seen objects in previous examples, so this concept should feel familiar.
The idea is simple:
- Create a Class.
- Create an Object from that class.
// Modelclass Car { constructor() { console.log("This is a car model"); }}
// Objectconst car = new Car();Why Store an Object in a Variable or Constant?
You might wonder:
- Why do we use
const? - Can we use
varinstead? - Can we create an object without storing it?
The answer is yes. All of the following are valid:
// Constantconst car = new Car();
// Variablevar car = new Car();
// Create directlynew Car();The difference isn’t about correctness—it’s about how you plan to use the object.
If you’ll use the object multiple times, storing it in a variable or constant makes your code cleaner and avoids repeating the creation step.
If you only need it once, creating it directly is perfectly acceptable.
Why Is const Commonly Used?
In most JavaScript codebases, you’ll see objects stored in const.
const car = new Car();This is considered a best practice because we usually don’t intend to reassign the variable to another object later.
Keep in mind that const does not make the object immutable. It only prevents the variable from referencing a different object.
What Does new Do?
The new keyword creates a new object from a class.
For example:
const car = new Car();This tells JavaScript to create a new instance of the Car class.
You can create as many objects as you want from the same class.
const car1 = new Car();const car2 = new Car();const car3 = new Car();const car4 = new Car();Each object is independent, even though they all come from the same class.
Using the car analogy, the Class is the blueprint, while every call to new Car() produces a brand-new car built from that blueprint.
Key Takeaways
- Objects are created from classes using the
newkeyword. - A single class can create any number of objects.
- Objects can be stored in
const,var, or created directly, depending on your needs. - Using
constis generally considered the best practice because object references are rarely reassigned.