1

I learned that it's good practice to declare variables before they are used. For example:

function myFunction() {
  var i;
  var j;
}

Does the same thing go for object properties, and is there a usual way/best practice for doing it? Because sometimes we don't know values of all properties when we declare the object. But if we don't declare them upfront (and set to null for example), it seems we can easily lose track of what's contained in the object:

function myFunction2() {
  var car = {};

  // some conditional code here fetching car data
  car.make = APIResponse.car.make;
  car.color = APIResponse.car.color;
  // even more code, before we can know another car property
  car.isLemon = isLemonAPIResponse;
}

Should it be instead something like:

function myFunction2() {
  var car = {
      make: APIResponse.car.make, // we can get this value right away so why not assign it
      color: APIResponse.car.color, // same here
      isLemon: null // this will be true or false once we find out, null means it's not set
  };
}

Or should it be some other way?

1

Neither is inherently better than the other. Use whichever one is more readable.

In your example, where you can get all the values immediately and construct the object in a single object literal expression, I see absolutely no reason not to do just that. If getting each property was a non-trivial exercise, or there are many properties the final object may or may not have, or there are objects within objects, then I'd be more inclined to start with var x = {}; and fill it in one property at a time.

It's important to point out that the reason people recommend declaring variables at the top of the function in Javascript is because of variable hoisting. In a nutshell, that means any variable declaration inside a function is effectively "hoisted" to the top of the function without you knowing about it. Personally, I still prefer to declare my variables in the scope where I actually use them, much like in C++, but at work we have linters like JSHint that will shout at me if I accidentally write code that refers to that variable outside the block scope I declared it in, or if I redeclare it in another block scope (a typical example would be writing two for loops that both start with var i = 0;), so in practice I never get bitten by this issue.

Because object members are not variables you declare, but rather properties of a variable, hoisting does not affect them in either of your examples, so it has no bearing on how you should construct your object. Only if you were constructing your object like var x = {}; var a = 2; x.prop = a; would "hoisting of members" become an issue.

1

There is no "correct" way of doing this as either will work just fine. It depends entirely upon what makes your code the easiest to write, understand and maintain.

If I already know the values of object properties at the time of declaration of the object, I find it generally makes the code a bit more self describing if I just put the known properties inside the initial object declaration.

If, on the other hand, a property name might need to be computed or there's significant code involved in figuring out what a property value might be, then I find the code is cleaner if I assign the property later in the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.