Javascript variable declaration question

Just a quick and maybe very daft question…

When leaving a variable value undeclared (to be determined later), which is better? If one is better than the other, is the reasoning conditional or is a matter of proper syntax, etc.?

let variable = ‘’;

or

let variable;

Thanks!

It’s best to declare the type if you know it, so the first option sets it to be a string, while the second option does not set a type at all, In general it’s best to initially declare it as whatever type (string, boolen, array, etc) that you want.

1 Like

I vote for

let variable;

because like the other guy said, let variable = ‘’; sets it to an empty string, just like let variable = 0; sets it to an int etc.

I’m a pretty new coder though so don’t take my word for it.

1 Like