Demo: https://tpkahlon.github.io/javascript/24
Source code: https://github.com/tpkahlon/javascript/blob/master/24/script.js
i can see executing a function like this (function(){})()
making your code hard to read once it gets more complicated
you can try window.onload at the end of your script to run the function when the window loads
the <body>
tag [mdn] also has an onload=''
attribute so you can run a function when the body loads
easy maintainability should be your highest priority in your code. these will help with that
1 Like