Skip to main content

Command Palette

Search for a command to run...

🧠 What does new do in JavaScript?

Published
1 min read

The new keyword is used to create a new instance of an object or class.

When you write:

const scene = new Scene(engine);

You are telling JavaScript:

“Hey, create a new Scene object, and use the Scene constructor function to set it up.”


.



🔍 Why do we need new here?

Because FreeCamera, Vector3, and Scene are all constructor functions (or classes) in Babylon.js.

Using new:

• Allocates memory for the new object

• Runs the constructor

• Sets up the object’s internal properties


🔧 Analogy:

const car = new Car();  // You just built a new car.

Without new, you’re just calling a function, not building an object — which often leads to errors or undefined behavior.


✅ Summary:

KeywordPurpose
newCreates a new instance of a class/object
Without newConstructor won’t work properly (or at all)

Let me know if you want a breakdown of how constructors work under the hood or how to make your own class using new!