Custom action fetches an external script and includes it in the page. So at run time the script will self execute as an anonymous function.
Following are few scenarios you may need this functionality in your SharePoint Projects:
Override the new sub site link under Site Contents with your site provisioning app URL
Redirect the user to specific page
Add dynamic html (global navigation or company copy rights information on the bottom of your page) content on your sharepoint page at run time without even modifying the actual master page.
In the below example, I have explained how to inject Jquery file to one of my web. The source files can be placed in CDN/File Share/Common Location such as App Catalog.
Please feel free to change the logic as per your requirement.
Note: The logic remains same for JSOM code also.