Or put all your code inside the document ready method callback. That is what it is there for.
The most common options are:
Put the script in the head and use the defer or async attribute.
Put the script after the page content, usually before the closing body tag.
Put the script anywhere in the document and wrap all the code in an event or async callback. In plain JS, it would be the load or DOMContentLoaded events.
The jQuery equivalence is most commonly just $() with a handler inside that contains all the code. When the handler/callback runs, you are guaranteed that the DOM is ready.