A question on constructor object

Hello @Grasshopper_Ben,
I created an object constructor like this:
piece = new component(30, 30, ‘red’, 10, 12);
function component() {};
This is not the entire code.
Why don’t we write ‘‘var piece’’ or ‘‘let piece’’?

Hey there,

Technically, you can declare a variable in Javascript without using a variable declarator. The computer will create it global variable (meaning it can be accessed everywhere in your code, which is called global scope).

It is very much not recommended. You don’t really want to have variables globally scoped, as it’s very easy to accidentally change the value of a global variable.

1 Like

Thanks for the answer. Is there a particular case though where it is needed to have a variable with a global scope?