The <code> tag is not part of the list of supported tags for formatBlock. You can however insert a <pre> tag by using:
document.execCommand('formatBlock', false, '<pre>');
You can check the w3c documentation for a list of supported tags (it depends on the browser).
If you do not care about IE, you can use the insertHTML option, combined together with document.getSelection(), like so:
document.execCommand('insertHTML', false, `<pre><code>${document.getSelection()}</code></pre>`)
Or you could implement the functionality yourself. As pointed out by others, document.execCommand is now obsolete, so it might be your safest option, depending on which browsers you need to support.
document.execCommandis obsolete according to developer.mozilla.org/en-US/docs/Web/API/Document/execCommand. "[...] its use is discouraged since it could be removed at any time. Try to avoid using it."