You can try something like this in default.xml
in:
/app/design/frontend/{vendor}/{theme}/Magento_Theme/layout/
Add this to the <body>
tag:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="before.body.end">
<block class="Magento\Framework\View\Element\Template" name="custom_inline_js" template="Magento_Theme::html/inline-js.phtml" before="-"/>
</referenceContainer>
</body>
</page>
Then create custom_inline_js.phtml
in:
/app/design/frontend/{vendor}/{theme}/Magento_Theme/templates/html/
Which contains all the custom js you need.
Also take into consideration that Magento 2 loads JS via RequireJS.
You can read more about it in the offical docs: http://devdocs.magento.com/guides/v2.2/javascript-dev-guide/javascript/js_overview.html
If your script depends on other JS libraries (like jQuery), you should use it.