frenchie
frenchie

Reputation: 51937

javascript testing length

If I have an object MyObject that contains an array of objects ListOfOtherObjects and I write this:

   if (MyObject.ListOfOtherObjects.length !== 0) {...}

to test and see if the array contains OtherObjects, is it the same as writing this:

   if (MyObject.ListOfOtherObjects) {...}

Thanks.

Upvotes: 0

Views: 423

Answers (1)

alex
alex

Reputation: 490233

Nope, you need to check the length property. An Array is always truthy, like any other Object.

You can, however, omit the explicit check of !== 0.

if (MyObject.ListOfOtherObjects.length) {...}

This condition will be true if the Array has at least one element.

Upvotes: 5

Related Questions