Elegantly check for null before method call
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;