does deep object exist control in nodejs

var session = {}
session.query = {}
session.query.username = "foo";

if( session.query.username )
    console.log('surname exists');

if( session.query2.username )
    console.log('surname exists');

this code throws this exception: "TypeError: Cannot read property 'query2' of undefined"

searched in stackoverflow and found this solution :

...
if( session && session.query2 && session.query2.username )
    console.log('surname exists')

but this is very long and I have very nested object's controls, so I can't use this method. Is there any short method that I can use?

This can work for you:

try {
    session.query.username &&
    console.log('Username exists');
} catch () {
    console.log('Username does not exist');
}

In case of very deep paths (or when you need to test many of them) it can be shorter.