magento add js before body end code example
Example: how to add script before in magento 2
Add referenceContainer code in layout file
app/design/frontend/<vendor>/<theme>/Magento_Theme/layout/default.xml
Before body end
<referenceContainer name="before.body.end">
<block class="Magento\Framework\View\Element\Js\Components" name="js.beforebody" as="jsbeforebody" template="Magento_Theme::js/js.phtml"/>
</referenceContainer>
For after body start
<referenceContainer name="after.body.start">
<block class="Magento\Framework\View\Element\Js\Components" name="js.afterbody" as="jsafterbody" template="Magento_Theme::js/js.phtml"/>
</referenceContainer>
Create js.phtml template file and put your js script/code in this file.
app/design/frontend/<vendor>/<theme>/Magento_Theme/templates/js/js.phtml