3

I'm working on some existing code that looks something like this:

return this.getMyObject() && this.getMyObject().myArray[0];

As far as I can tell it is checking that the object returned by getMyObject() exists before returning the first item in it's myArray property. How does this work and is it good form?

Update: The reason for my question came from the confusion over how the && operator can be used to return a property value and not a boolean result. After further thought, to make it more readable I refactored the line to:

return this.getMyObject() ? this.getMyObject().myArray[0] : undefined;

Obviously I am assuming here that the myArray property will exist.

Jim
  • 1,253
  • 1
  • 11
  • 13
  • You can use `typeof` ([documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)) – Alon Eitan Oct 23 '15 at 14:57
  • [Related](http://stackoverflow.com/questions/3163407/javascript-and-operator-within-assignment) - it explains the use of the `&&` operator in this sort of situation. – James Thorpe Oct 23 '15 at 14:57

2 Answers2

0

It's correct form. If there is no object returned by this.getMyObject() then function will return false in another case second part of condition will be executed and returned as a result of function. It's good practice to check if object exists before calling any method on it, because an error could occur if not to do so.

But you should check an existence of an object only if you are not sure whether it exists.

Yuriy Yakym
  • 2,339
  • 12
  • 24
0

That code works because of type coercion. Some people will tell you its good and some people will say always truly check something using typeof

if (typeof someVariable === 'undefined')

Even in examples below the above check isn't enough. I don't know what is better but that code as far as I am concerned isn't how I write it myself but it is accepted with a lot of javascript developers. There are times that code in the correct conditions can still pass the first check and yet throw an error accessing the property. Depends how controlled your situation is that determines, to me, if you should or shouldn't allow it.

Example of passing first check and failing:

var myObject = 1;
var test = myObject && myObject.myArray[0];

Or as @JamesThorpe pointed out in comment above:

var myObject = {};
var test = myObject && myObject.myArray[0];

Also people familiar with some coding languages but not JS might look at that code and not understand what it means where checking with an if and then returning the value might be a bit more readable to others, which is also a plus I think.

AtheistP3ace
  • 9,133
  • 12
  • 41
  • 40
  • @JamesThorpe Oh I am sorry. I thought I saw you saw if getMyObject returns {} it will error. Either way it does. I will remove the reference of you if thats not what you said or meant. I apologize. – AtheistP3ace Oct 23 '15 at 15:09
  • 1
    I see what you're saying now - it's another example of it failing... removed my comment :) – James Thorpe Oct 23 '15 at 15:10