That’s because the jQuery script with the defer attribute loads after the page processing is done, while the inline script is processed during the page load.
You can either remove the defer attribute (wouldn’t prefer), use type=module instead of type=text/javascript, or register jQuery as resource and convert the inline script <dot:InlineScript Dependencies=jquery />