Elegantly check for null before method call How do I elegantly check for null before calling a method on an object? This is how I do it right now:

var title = document.querySelector('title');
title = title ? title.text : '';


Null Object pattern would be nice in this case but I don't own the document.querySelector code.
You can do this:

var title = document.querySelector('title') || {text:''};


then just use title.text in-line.

Alternatively, you could use this one-liner:

var title = (document.querySelector('title') || {text:''}).text;