execCommand()
returns true
if the command is successful, or false
otherwise. Hence you can do something like this:
if (!document.execCommand('FormatBlock', null, 'H1')) {
document.execCommand('FormatBlock', null, '<H1>');
}
Though other browsers seem also to work with <H1>
as well...