2

I have some objects with properties. I wanted to test to see if they had characters in them so I initially wrote this:

if (MyObject.Prop1.length > 0) {....}

However, sometimes the object may not have a certain property so I was getting the error "cannot get length".

I changed it by writing this:

if (MyObject.Prop1 && MyObject.Prop1.length > 0) {....}

I'm using the chrome inspector and when I run the code, I don't get the error anymore. Is this going to work in every browser?

Thanks.

5
  • 3
    Are you sure Prop1 will always be a string? Because now, if it contains the Number 0, your condition will be false. And numbers don't have a length property also. Commented Dec 7, 2011 at 22:52
  • @baz: well if it contains 0 then the length won't be larger than zero and thus the call probably shouldn't be made anyway Commented Dec 7, 2011 at 22:57
  • @Martin All depends on the logic of the application :). Commented Dec 7, 2011 at 22:59
  • What happens when we have this: MyObject.Prop1 = 4; ? I only have strings ... for now, but's still WIP. Commented Dec 7, 2011 at 23:05
  • @baz: nevermind i am tired and need sleep, sorry for my useless and very wrong comment. Commented Dec 7, 2011 at 23:07

3 Answers 3

5

As an alternative:

if ('Prop1' in MyObject && MyObject.Prop1.length > 0) { ... )

Or, to be even more careful:

if (MyObject.hasOwnProperty('Prop1') && MyObject.Prop1.length > 0) { ... }

Of course that might be the wrong thing to do, depending on the nature of "MyObject".

Sign up to request clarification or add additional context in comments.

Comments

2

Yes it will work quite fine, although you can save yourself the > 0 and just do

if (MyObject.Prop1 && MyObject.Prop1.length) {....}

since anything other than zero will evaluate to true.

2 Comments

What if one day Prop1 becomes an integer? How should I change this?
@frenchie Pointy's and Jordan's answer gives some clues. Maybe ask a different question.
2

Since undefined, 0, and "" are all "falsy" in JavaScript, this is equivalent:

if(MyObject.Prop1) {
  // ...
}

Rick Waldron's "Idiomatic JavaScript" is a good reference for simplifying conditional statements without sacrificing correctness. You can test its use yourself:

function testProp(val) {
  if(val) {
    return val.length;
  }

  return "nope!";
}

var myObj = { stringProp   : "foo",
              emptyStrProp : "",
              // undefinedProp is not defined
              zeroProp     : 0,
              twelveProp   : 12,
              otherObjProp : document.createElement('div'),
              arrayProp    : [ 'a', 'b' ]
            };

console.log( testProp( myObj.stringProp    ) ); // => 3
console.log( testProp( myObj.emptyStrProp  ) ); // => "nope!"
console.log( testProp( myObj.undefinedProp ) ); // => "nope!"

// of course if you're expecting values other than strings and undefined
// you'll have to account for them

console.log( testProp( myObj.zeroProp      ) ); // => "nope!"
console.log( testProp( myObj.twelveProp    ) ); // => undefined
console.log( testProp( myObj.otherObjProp  ) ); // => undefined
console.log( testProp( myObj.arrayProp     ) ); // => 2

1 Comment

To clarify, 0 is "falsy" and all other numbers are "truthy," so if prop === 0 then if(prop) will evaluate it as false, and if prop == 1 or 5 or -10 or 3.333 then if(prop) will come up true. If you evaluate prop.length where prop is equal to anything without a length property (including numbers), though, you will get undefined, which is falsy. I hope that's helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.