How to defer JavaScript execution in Magento 2

- Magento 2

Magento 2 Development

Being a boolean attribute, defer specifies that the script is executed after the page parsing. The attribute is aimed at external scripts only (the src attribute should be present). Find more information about defer here.

Despite Magento 2 supports standard script deferring, it never uses defer in its core. To change the situation, you can use the following code:

More tips from Magento 2 Developer’s Cookbook

Source