Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring an object in JavaScript for later use - null or {}?

Tags:

javascript

Simple question, What is the best practise, should I declare JavaScript objects, that will not be initialized until some later stage, with null or {}?

like image 915
csss Avatar asked Nov 11 '22 11:11

csss


1 Answers

I think it really depends. If you just want to declare it early because you know it is "hoisted" to the beginning of the function anyway, and will initialize a few lines down below, then just use

var foo;

which makes foo undefined. Otherwise, if you need to do anything with foo being an object before your initialization, then it will be better to use

var foo = {};
like image 184
nonopolarity Avatar answered Nov 14 '22 21:11

nonopolarity